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

Use ActiveEnum field as composite primary key #1414

Merged
merged 3 commits into from
Jan 30, 2023

Conversation

billy1624
Copy link
Member

PR Info

Breaking Changes

  • Provided a blanket implementation of TryFromU64 for all ActiveEnum
impl<T> TryFromU64 for T
where
    T: ActiveEnum,
{
    fn try_from_u64(_: u64) -> Result<Self, DbErr> {
        Err(DbErr::ConvertFromU64(
            "Fail to construct ActiveEnum from a u64, if your primary key consist of a ActiveEnum field, its auto increment should be set to false."
        ))
    }
}

@billy1624 billy1624 self-assigned this Jan 21, 2023
@billy1624 billy1624 marked this pull request as ready for review January 21, 2023 05:56
@billy1624 billy1624 requested a review from tyt2y3 January 21, 2023 05:56
Comment on lines +59 to +65
let _ = ActiveModel {
category: Set(Some(Category::Big)),
color: Set(Some(Color::Black)),
..model.into_active_model()
}
.save(db)
.await?;
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the intention of this bit, and I think it SHOULD fail to insert instead of passing

Copy link
Member Author

Choose a reason for hiding this comment

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

It does an update instead of insert.

// The `..model.into_active_model()` expands into

let _ = ActiveModel {
        category: Set(Some(Category::Big)),
        color: Set(Some(Color::Black)),
        id: Unchanged( ... )
    }
    .save(db)
    .await?;

Copy link
Member

@tyt2y3 tyt2y3 Jan 30, 2023

Choose a reason for hiding this comment

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

Oh Right

Copy link
Member

@tyt2y3 tyt2y3 Jan 30, 2023

Choose a reason for hiding this comment

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

Can we also add an insert conflict test case

Copy link
Member Author

Choose a reason for hiding this comment

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

We can insert a row with duplicate ID. But I cannot assert the exact error, since it's database dependent. 66cffd4

Copy link
Member

Choose a reason for hiding this comment

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

Yes, is_err would do

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, good to go

@tyt2y3 tyt2y3 added this to the 0.11.x milestone Jan 29, 2023
@billy1624 billy1624 requested a review from tyt2y3 January 30, 2023 10:42
Copy link
Member

@tyt2y3 tyt2y3 left a comment

Choose a reason for hiding this comment

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

Cool

@tyt2y3 tyt2y3 merged commit a6bd91a into master Jan 30, 2023
@tyt2y3 tyt2y3 deleted the active-enum-as-primary-key branch January 30, 2023 13:47
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.

Composite primary key with enum causes generated entity to fail to compile
2 participants