Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support composite primary key of length up to 12 #1508

Merged
merged 6 commits into from
Apr 13, 2023
Merged

Conversation

billy1624
Copy link
Member

PR Info

New Features

  • Implemented IntoIdentity for Identity
  • Identity supports up to identity tuple of DynIden with length up to 12
  • Implemented IntoIdentity for tuple of IdenStatic with length up to 12
  • Implemented IdentityOf for tuple of ColumnTrait with length up to 12
  • Implemented TryGetableMany for tuple of TryGetable with length up to 12
  • Implemented TryFromU64 for tuple of TryFromU64 with length up to 12
  • Supports entity with composite primary key of length 12
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "primary_key_of_12")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_1: String,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_2: i8,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_3: u8,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_4: i16,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_5: u16,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_6: i32,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_7: u32,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_8: i64,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_9: u64,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_10: f32,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_11: f64,
    #[sea_orm(primary_key, auto_increment = false)]
    pub id_12: bool,
    pub owner: String,
    pub name: String,
    pub description: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

Breaking Changes

  • Added Identity::Many

@billy1624 billy1624 self-assigned this Feb 24, 2023
@billy1624 billy1624 marked this pull request as ready for review March 22, 2023 10:06
@billy1624 billy1624 requested a review from tyt2y3 March 22, 2023 10:06
@@ -101,6 +101,28 @@ where
.add(f(c2, v2)),
)
.add(f(c1, v1)),
(Identity::Many(col_vec), ValueTuple::Many(value_vec))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow... I was thinking, how did you come up with this algorithm?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some test cases to ensure its correctness. Since it's originally written by you can you please double check it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied it from stack overflow... umm actually from issue #1209

Comment on lines 784 to 791
r#"SELECT "m"."w", "m"."x", "m"."y", "m"."z""#,
r#"FROM "m""#,
r#"WHERE ("m"."w" = $1 AND "m"."x" = $2 AND "m"."y" = $3 AND "m"."z" > $4)"#,
r#"OR ("m"."w" = $5 AND "m"."x" = $6 AND "m"."y" > $7)"#,
r#"OR ("m"."w" = $8 AND "m"."x" > $9)"#,
r#"OR "m"."w" > $10"#,
r#"ORDER BY "m"."w" ASC, "m"."x" ASC, "m"."y" ASC, "m"."z" ASC"#,
r#"LIMIT $11"#,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we verify this is actually correct

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An induction proof? e933b57

Assume the logic of Many is correct. Then, Many with its inner Vec of length 1, 2 and 3 should produce the same SQL of it counterpart One, Two, Three.

E.g. for One, both should yield the same SQL statement.

Entity::find()
    .cursor_by(Column::Id)
    .before(10)
    .first(2)
Entity::find()
    .cursor_by(Identity::Many(vec![Column::Id.into_iden()]))
    .before(ValueTuple::Many(vec![10.into()]))
    .first(2)

Repeat the assertion for Two and Three as well.

Copy link
Member

@tyt2y3 tyt2y3 Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but that's only the basis of the induction. I am more concerned with why it should be correct from 4 onwards.

Comment on lines +119 to +125
// WHERE
// (col_1 = val_1 AND col_2 = val_2 AND ... AND col_n > val_n)
// OR (col_1 = val_1 AND col_2 = val_2 AND ... AND col_n-1 > val_n-1)
// OR (col_1 = val_1 AND col_2 = val_2 AND ... AND col_n-2 > val_n-2)
// OR ...
// OR (col_1 = val_1 AND col_2 > val_2)
// OR (col_1 > val_1)
Copy link
Member

@tyt2y3 tyt2y3 Apr 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greatly appreciated! 💯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comprehensive testing on its way :P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this. The algorithm in action. 2bec23a

@billy1624 billy1624 merged commit d45bb5b into master Apr 13, 2023
@billy1624 billy1624 deleted the identity-many branch April 13, 2023 08:18
darkmmon added a commit to darkmmon/seaql.github.io that referenced this pull request Jul 11, 2023
billy1624 added a commit to SeaQL/seaql.github.io that referenced this pull request Jul 19, 2023
* Optional Field SeaQL/sea-orm#1513

* .gitignore SeaQL/sea-orm#1334

* migration table custom name SeaQL/sea-orm#1511

* OR condition relation SeaQL/sea-orm#1433

* DerivePartialModel SeaQL/sea-orm#1597

* space for migration file naming SeaQL/sea-orm#1570

* composite key up to 12 SeaQL/sea-orm#1508

* seaography integration SeaQL/sea-orm#1599

* QuerySelect SimpleExpr SeaQL/sea-orm#1702

* sqlErr SeaQL/sea-orm#1707

* migration check SeaQL/sea-orm#1519

* postgres array SeaQL/sea-orm#1565

* param intoString SeaQL/sea-orm#1439

* **skipped** re-export SeaQL/sea-orm#1661

* ping SeaQL/sea-orm#1627

* on empty do nothing SeaQL/sea-orm#1708

* on conflict do nothing SeaQL/sea-orm#1712

* **skipped** upgrade versions

* active enum fail safe SeaQL/sea-orm#1374

* relation generation check SeaQL/sea-orm#1435

* entity generation bug SeaQL/sea-schema#105

* **skipped** bug fix that does not require edits

* EnumIter change SeaQL/sea-orm#1535

* completed and fixed a previous todo SeaQL/sea-orm#1570

* amended wordings and structures

* Edit

* Remove temp file

---------

Co-authored-by: Billy Chan <[email protected]>
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

🎉 Released In 0.12.1 🎉

Thank you everyone for the contribution!
This feature is now available in the latest release. Now is a good time to upgrade!
Your participation is what makes us unique; your adoption is what drives us forward.
You can support SeaQL 🌊 by starring our repos, sharing our libraries and becoming a sponsor ⭐.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

large composite key table
2 participants