Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = [".", "sea-orm-macros", "sea-orm-codegen", "examples/quickstart"]
members = [".", "sea-orm-macros", "sea-orm-codegen"]

[package]
authors = ["Chris Tsang <chris.2y3@outlook.com>"]
Expand Down
3 changes: 3 additions & 0 deletions examples/quickstart/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[workspace]
# A separate workspace

[package]
edition = "2024"
name = "sea-orm-quickstart"
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn get_full_migration_dir(migration_dir: &str) -> PathBuf {

fn create_new_migration(migration_name: &str, migration_dir: &str) -> Result<(), Box<dyn Error>> {
let migration_filepath =
get_full_migration_dir(migration_dir).join(format!("{}.rs", &migration_name));
get_full_migration_dir(migration_dir).join(format!("{migration_name}.rs"));
println!("Creating migration file `{}`", migration_filepath.display());
// TODO: make OS agnostic
let migration_template =
Expand Down
18 changes: 4 additions & 14 deletions sea-orm-macros/src/derives/active_model_ex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,13 @@ pub fn expand_derive_active_model_ex(
));
}
}
if compound_attrs.self_ref.is_some()
&& compound_attrs.via.is_some()
&& compound_attrs.reverse.is_none()
if let (Some(()), Some(via)) =
(&compound_attrs.self_ref, &compound_attrs.via)
{
has_many_via_self_fields.push((
ident.clone(),
compound_attrs.via.as_ref().unwrap().value(),
false,
));
} else if compound_attrs.self_ref.is_some()
&& compound_attrs.via.is_some()
&& compound_attrs.reverse.is_some()
{
has_many_via_self_fields.push((
ident.clone(),
compound_attrs.via.as_ref().unwrap().value(),
true,
via.value(),
compound_attrs.reverse.is_some(),
));
}
}
Expand Down
30 changes: 21 additions & 9 deletions sea-orm-macros/src/derives/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ struct DeriveEntity {
relation_ident: syn::Ident,
schema_name: Option<syn::LitStr>,
table_name: Option<syn::LitStr>,
unit_struct: bool,
}

impl DeriveEntity {
fn new(input: syn::DeriveInput) -> Result<Self, syn::Error> {
let sea_attr = derive_attr::SeaOrm::try_from_attributes(&input.attrs)?.unwrap_or_default();

let unit_struct = matches!(
&input.data,
syn::Data::Struct(syn::DataStruct {
fields: syn::Fields::Unit,
..
})
);

let ident = input.ident;
let column_ident = sea_attr.column.unwrap_or_else(|| format_ident!("Column"));
let model_ident = sea_attr.model.unwrap_or_else(|| format_ident!("Model"));
Expand Down Expand Up @@ -53,6 +62,7 @@ impl DeriveEntity {
relation_ident,
schema_name,
table_name,
unit_struct,
})
}

Expand Down Expand Up @@ -161,17 +171,19 @@ impl DeriveEntity {
}

fn impl_entity_registry(&self) -> TokenStream {
if cfg!(feature = "entity-registry") {
quote! {
sea_orm::register_entity! {
sea_orm::EntityRegistry {
module_path: module_path!(),
schema_info: |schema| sea_orm::EntitySchemaInfo::new(Entity, schema),
}
// An Entity that carries data (e.g. a runtime table name) has no static schema, and
// cannot be named as a value here either, so there is nothing to register.
if !cfg!(feature = "entity-registry") || !self.unit_struct {
return quote!();
}
let ident = &self.ident;
quote! {
sea_orm::register_entity! {
sea_orm::EntityRegistry {
module_path: module_path!(),
schema_info: |schema| sea_orm::EntitySchemaInfo::new(#ident, schema),
}
}
} else {
quote!()
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions sea-orm-macros/src/derives/model_ex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,14 @@ fn relation_enum_variant(attr: &compound_attr::SeaOrm, ty: &str) -> Option<Token
#[sea_orm(#belongs_to = "Entity", from = #from, to = #to, #extra)]
#relation_enum
})
} else if attr.self_ref.is_some()
&& attr.via.is_none()
&& attr.relation_reverse.is_some()
&& ty.starts_with("HasMany<")
{
} else if let (Some(()), None, Some(relation_reverse), true) = (
&attr.self_ref,
&attr.via,
&attr.relation_reverse,
ty.starts_with("HasMany<"),
) {
let has_many = Ident::new("has_many", Span::call_site());
let via_rel = format!(
"Relation::{}",
attr.relation_reverse.as_ref().unwrap().value()
);
let via_rel = format!("Relation::{}", relation_reverse.value());

Some(quote! {
#[doc = " Generated by sea-orm-macros"]
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-sync/src/database/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ impl fmt::Display for Statement {
inject_parameters(&self.sql, &values.0, &SqliteQueryBuilder)
}
};
write!(f, "{}", &string)
write!(f, "{string}")
}
None => {
write!(f, "{}", &self.sql)
write!(f, "{}", self.sql)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions sea-orm-sync/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ mod primary_key;
mod registry;
mod relation;

/// Registers an Entity with the Entity Registry.
///
/// This no-op fallback is used when the `entity-registry` feature is disabled, so that
/// the code emitted by `DeriveEntity` compiles no matter how the feature is resolved
/// for `sea-orm-macros`.
#[cfg(not(feature = "entity-registry"))]
#[macro_export]
macro_rules! register_entity {
($($entity:tt)*) => {};
}

pub use active_enum::*;
pub use active_model::*;
pub use active_model_ex::*;
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-sync/src/query/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ pub(crate) fn join_tbl_on_condition(
foreign_keys: Identity,
) -> Condition {
let mut cond = Condition::all();
for (owner_key, foreign_key) in owner_keys.into_iter().zip(foreign_keys.into_iter()) {
for (owner_key, foreign_key) in owner_keys.into_iter().zip(foreign_keys) {
cond = cond
.add(Expr::col((from_tbl.clone(), owner_key)).equals((to_tbl.clone(), foreign_key)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/database/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ impl fmt::Display for Statement {
inject_parameters(&self.sql, &values.0, &SqliteQueryBuilder)
}
};
write!(f, "{}", &string)
write!(f, "{string}")
}
None => {
write!(f, "{}", &self.sql)
write!(f, "{}", self.sql)
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ mod primary_key;
mod registry;
mod relation;

/// Registers an Entity with the Entity Registry.
///
/// This no-op fallback is used when the `entity-registry` feature is disabled, so that
/// the code emitted by `DeriveEntity` compiles no matter how the feature is resolved
/// for `sea-orm-macros`.
#[cfg(not(feature = "entity-registry"))]
#[macro_export]
macro_rules! register_entity {
($($entity:tt)*) => {};
}

pub use active_enum::*;
pub use active_model::*;
pub use active_model_ex::*;
Expand Down
2 changes: 1 addition & 1 deletion src/query/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ pub(crate) fn join_tbl_on_condition(
foreign_keys: Identity,
) -> Condition {
let mut cond = Condition::all();
for (owner_key, foreign_key) in owner_keys.into_iter().zip(foreign_keys.into_iter()) {
for (owner_key, foreign_key) in owner_keys.into_iter().zip(foreign_keys) {
cond = cond
.add(Expr::col((from_tbl.clone(), owner_key)).equals((to_tbl.clone(), foreign_key)));
}
Expand Down
Loading