Skip to content

Commit 5f553d8

Browse files
authored
macros: Set auto_increment to false for String/Uuid pk by default (#2881)
1 parent 2b60ac3 commit 5f553d8

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

sea-orm-macros/src/derives/entity_model.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use syn::{
1111
Attribute, Data, Fields, Lit, LitStr, punctuated::Punctuated, spanned::Spanned, token::Comma,
1212
};
1313

14+
const NOT_AUTO_INCRE_TYPE_SUFFIX: [&str; 2] = ["String", "Uuid"];
15+
1416
#[allow(dead_code)]
1517
fn convert_case(s: &str, case_style: CaseStyle) -> String {
1618
match case_style {
@@ -164,7 +166,7 @@ pub fn expand_derive_entity_model(data: &Data, attrs: &[Attribute]) -> syn::Resu
164166
let mut columns_save_as: Punctuated<_, Comma> = Punctuated::new();
165167
let mut primary_keys: Punctuated<_, Comma> = Punctuated::new();
166168
let mut primary_key_types: Punctuated<_, Comma> = Punctuated::new();
167-
let mut auto_increment = true;
169+
let mut auto_increment: Option<bool> = None;
168170
#[cfg(feature = "with-json")]
169171
let mut columns_json_keys: Punctuated<_, Comma> = Punctuated::new();
170172

@@ -240,7 +242,7 @@ pub fn expand_derive_entity_model(data: &Data, attrs: &[Attribute]) -> syn::Resu
240242
let lit = meta.value()?.parse()?;
241243
if let Lit::Bool(litbool) = lit {
242244
is_auto_increment = litbool.value();
243-
auto_increment = litbool.value();
245+
auto_increment = Some(litbool.value());
244246
} else {
245247
return Err(
246248
meta.error(format!("Invalid auto_increment = {lit:?}"))
@@ -455,6 +457,15 @@ pub fn expand_derive_entity_model(data: &Data, attrs: &[Attribute]) -> syn::Resu
455457
};
456458
let field_span = field.span();
457459

460+
if is_primary_key && auto_increment.is_none() {
461+
for suffix in NOT_AUTO_INCRE_TYPE_SUFFIX {
462+
if field_type.ends_with(suffix) {
463+
auto_increment = Some(false);
464+
break;
465+
}
466+
}
467+
}
468+
458469
let sea_query_col_type =
459470
super::value_type_match::column_type_expr(sql_type, field_type, field_span);
460471

@@ -518,7 +529,10 @@ pub fn expand_derive_entity_model(data: &Data, attrs: &[Attribute]) -> syn::Resu
518529
}
519530

520531
let primary_key = {
521-
let auto_increment = auto_increment && primary_keys.len() == 1;
532+
let auto_increment = match auto_increment {
533+
Some(value) => value && primary_keys.len() == 1,
534+
None => primary_keys.len() == 1,
535+
};
522536
let primary_key_types = if primary_key_types.len() == 1 {
523537
let first = primary_key_types.first();
524538
quote! { #first }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use sea_orm::entity::prelude::*;
2+
use sea_orm_macros::DeriveEntityModel;
3+
4+
mod string_pk {
5+
use super::*;
6+
7+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
8+
#[sea_orm(table_name = "string_pk")]
9+
pub struct Model {
10+
#[sea_orm(primary_key)]
11+
pub id: String,
12+
}
13+
14+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
15+
pub enum Relation {}
16+
17+
impl ActiveModelBehavior for ActiveModel {}
18+
}
19+
20+
mod string_pk_set_true {
21+
use super::*;
22+
23+
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
24+
#[sea_orm(table_name = "string_pk")]
25+
pub struct Model {
26+
#[sea_orm(primary_key, auto_increment = true)]
27+
pub id: String,
28+
}
29+
30+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
31+
pub enum Relation {}
32+
33+
impl ActiveModelBehavior for ActiveModel {}
34+
}
35+
36+
#[test]
37+
fn test_auto_increment_default_by_type() {
38+
assert!(!string_pk::PrimaryKey::auto_increment());
39+
assert!(string_pk_set_true::PrimaryKey::auto_increment());
40+
}

0 commit comments

Comments
 (0)