Skip to content

Commit

Permalink
Introduce ARITY to PrimaryKeyTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Mar 29, 2024
1 parent e34f09d commit cc4f45b
Show file tree
Hide file tree
Showing 21 changed files with 61 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub user_id: Option<i32> ,
pub user_id: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -15,7 +15,7 @@ pub enum Relation {
from = "Column::UserId",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction",
on_delete = "NoAction"
)]
Users,
#[sea_orm(has_many = "super::users_saved_bills::Entity")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub enum Relation {
from = "Column::BillId",
to = "super::bills::Column::Id",
on_update = "Cascade",
on_delete = "Cascade",
on_delete = "Cascade"
)]
Bills,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::UserId",
to = "super::users::Column::Id",
on_update = "Cascade",
on_delete = "Cascade",
on_delete = "Cascade"
)]
Users,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ pub enum Relation {
from = "Column::BillId",
to = "super::bills::Column::Id",
on_update = "Cascade",
on_delete = "Cascade",
on_delete = "Cascade"
)]
Bills,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::UserId",
to = "super::users::Column::Id",
on_update = "Cascade",
on_delete = "Cascade",
on_delete = "Cascade"
)]
Users,
}
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/src/tests_cfg/many_to_many/bills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub user_id: Option<i32> ,
pub user_id: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -15,7 +15,7 @@ pub enum Relation {
from = "Column::UserId",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction",
on_delete = "NoAction"
)]
Users,
#[sea_orm(has_many = "super::users_votes::Entity")]
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/src/tests_cfg/many_to_many/users_votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ pub enum Relation {
from = "Column::BillId",
to = "super::bills::Column::Id",
on_update = "Cascade",
on_delete = "Cascade",
on_delete = "Cascade"
)]
Bills,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::UserId",
to = "super::users::Column::Id",
on_update = "Cascade",
on_delete = "Cascade",
on_delete = "Cascade"
)]
Users,
}
Expand Down
4 changes: 2 additions & 2 deletions sea-orm-codegen/src/tests_cfg/many_to_many_multiple/bills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub user_id: Option<i32> ,
pub user_id: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -15,7 +15,7 @@ pub enum Relation {
from = "Column::UserId",
to = "super::users::Column::Id",
on_update = "NoAction",
on_delete = "NoAction",
on_delete = "NoAction"
)]
Users,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ pub struct Model {
pub user_id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub bill_id: i32,
pub user_idd: Option<i32> ,
pub bill_idd: Option<i32> ,
pub user_idd: Option<i32>,
pub bill_idd: Option<i32>,
pub vote: bool,
}

Expand All @@ -17,25 +17,25 @@ pub enum Relation {
#[sea_orm(
belongs_to = "super::bills::Entity",
from = "Column::BillIdd",
to = "super::bills::Column::Id",
to = "super::bills::Column::Id"
)]
Bills2,
#[sea_orm(
belongs_to = "super::bills::Entity",
from = "Column::BillId",
to = "super::bills::Column::Id",
to = "super::bills::Column::Id"
)]
Bills1,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::UserIdd",
to = "super::users::Column::Id",
to = "super::users::Column::Id"
)]
Users2,
#[sea_orm(
belongs_to = "super::users::Entity",
from = "Column::UserId",
to = "super::users::Column::Id",
to = "super::users::Column::Id"
)]
Users1,
}
Expand Down
8 changes: 2 additions & 6 deletions sea-orm-codegen/src/tests_cfg/self_referencing/bills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub self_id: Option<i32> ,
pub self_id: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "Entity",
from = "Column::SelfId",
to = "Column::Id",
)]
#[sea_orm(belongs_to = "Entity", from = "Column::SelfId", to = "Column::Id")]
SelfRef,
}

Expand Down
16 changes: 4 additions & 12 deletions sea-orm-codegen/src/tests_cfg/self_referencing/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,15 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub self_id: Option<i32> ,
pub self_idd: Option<i32> ,
pub self_id: Option<i32>,
pub self_idd: Option<i32>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "Entity",
from = "Column::SelfId",
to = "Column::Id",
)]
#[sea_orm(belongs_to = "Entity", from = "Column::SelfId", to = "Column::Id")]
SelfRef2,
#[sea_orm(
belongs_to = "Entity",
from = "Column::SelfIdd",
to = "Column::Id",
)]
#[sea_orm(belongs_to = "Entity", from = "Column::SelfIdd", to = "Column::Id")]
SelfRef1,
}

Expand Down
3 changes: 3 additions & 0 deletions sea-orm-macros/src/derives/entity_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res

let primary_key = {
let auto_increment = auto_increment && primary_keys.len() == 1;
let arity = primary_key_types.len();
let primary_key_types = if primary_key_types.len() == 1 {
let first = primary_key_types.first();
quote! { #first }
Expand All @@ -323,6 +324,8 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res

#[automatically_derived]
impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = #arity;

type ValueType = #primary_key_types;

fn auto_increment() -> bool {
Expand Down
12 changes: 12 additions & 0 deletions src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ mod tests {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down Expand Up @@ -832,6 +834,8 @@ mod tests {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down Expand Up @@ -943,6 +947,8 @@ mod tests {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down Expand Up @@ -1053,6 +1059,8 @@ mod tests {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down Expand Up @@ -1184,6 +1192,8 @@ mod tests {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down Expand Up @@ -1322,6 +1332,8 @@ mod tests {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
///
/// // Or implement the [PrimaryKeyTrait] manually instead of using the macro [DerivePrimaryKey]
/// impl PrimaryKeyTrait for PrimaryKey {
/// const ARITY: usize = 1;
///
/// type ValueType = i32;
///
/// fn auto_increment() -> bool {
Expand Down
7 changes: 5 additions & 2 deletions src/entity/primary_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ use std::fmt::Debug;
/// ```
/// See module level docs [crate::entity] for a full example
pub trait PrimaryKeyTrait: IdenStatic + Iterable {
#[allow(missing_docs)]
/// How many columns this Primary Key comprises
const ARITY: usize;

/// Rust Type of the Primary Key
type ValueType: Sized
+ Send
+ Debug
Expand All @@ -48,7 +51,7 @@ pub trait PrimaryKeyTrait: IdenStatic + Iterable {
+ TryGetableMany
+ TryFromU64;

/// Method to call to perform `AUTOINCREMENT` operation on a Primary Kay
/// Method to call to perform `AUTOINCREMENT` operation on a Primary Key
fn auto_increment() -> bool;
}

Expand Down
2 changes: 2 additions & 0 deletions src/tests_cfg/cake_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/tests_cfg/cake_filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 2;

type ValueType = (i32, i32);

fn auto_increment() -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/tests_cfg/cake_filling_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 2;

type ValueType = (i32, i32);

fn auto_increment() -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/tests_cfg/filling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/tests_cfg/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down
2 changes: 2 additions & 0 deletions src/tests_cfg/lunch_set_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down
1 change: 1 addition & 0 deletions tests/common/features/collection_expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;
type ValueType = i32;
fn auto_increment() -> bool {
true
Expand Down
2 changes: 2 additions & 0 deletions tests/common/features/dyn_table_name_lazy_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub enum PrimaryKey {
}

impl PrimaryKeyTrait for PrimaryKey {
const ARITY: usize = 1;

type ValueType = i32;

fn auto_increment() -> bool {
Expand Down

0 comments on commit cc4f45b

Please sign in to comment.