-
-
Notifications
You must be signed in to change notification settings - Fork 520
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use super::sea_orm_active_enums::*; | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "teas")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: Tea, | ||
pub category: Option<Category>, | ||
pub color: Option<Color>, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
pub mod common; | ||
|
||
pub use common::{features::*, setup::*, TestContext}; | ||
use pretty_assertions::assert_eq; | ||
use sea_orm::{ | ||
entity::prelude::*, | ||
entity::*, | ||
sea_query::{BinOper, Expr}, | ||
ActiveEnum as ActiveEnumTrait, DatabaseConnection, | ||
}; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(any( | ||
feature = "sqlx-mysql", | ||
feature = "sqlx-sqlite", | ||
feature = "sqlx-postgres" | ||
))] | ||
async fn main() -> Result<(), DbErr> { | ||
let ctx = TestContext::new("enum_primary_key_tests").await; | ||
create_tables(&ctx.db).await?; | ||
insert_teas(&ctx.db).await?; | ||
ctx.delete().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn insert_teas(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
use teas::*; | ||
|
||
let model = Model { | ||
id: Tea::EverydayTea, | ||
category: None, | ||
color: None, | ||
}; | ||
|
||
assert_eq!( | ||
model, | ||
ActiveModel { | ||
id: Set(Tea::EverydayTea), | ||
category: Set(None), | ||
color: Set(None), | ||
..Default::default() | ||
} | ||
.insert(db) | ||
.await? | ||
); | ||
assert_eq!(model, Entity::find().one(db).await?.unwrap()); | ||
assert_eq!( | ||
model, | ||
Entity::find() | ||
.filter(Column::Id.is_not_null()) | ||
.filter(Column::Category.is_null()) | ||
.filter(Column::Color.is_null()) | ||
.one(db) | ||
.await? | ||
.unwrap() | ||
); | ||
|
||
let _ = ActiveModel { | ||
category: Set(Some(Category::Big)), | ||
color: Set(Some(Color::Black)), | ||
..model.into_active_model() | ||
} | ||
.save(db) | ||
.await?; | ||
|
||
let model = Entity::find().one(db).await?.unwrap(); | ||
assert_eq!( | ||
model, | ||
Model { | ||
id: Tea::EverydayTea, | ||
category: Some(Category::Big), | ||
color: Some(Color::Black), | ||
} | ||
); | ||
assert_eq!( | ||
model, | ||
Entity::find() | ||
.filter(Column::Id.eq(Tea::EverydayTea)) | ||
.filter(Column::Category.eq(Category::Big)) | ||
.filter(Column::Color.eq(Color::Black)) | ||
.one(db) | ||
.await? | ||
.unwrap() | ||
); | ||
assert_eq!( | ||
model, | ||
Entity::find() | ||
.filter( | ||
Expr::col(Column::Id) | ||
.binary(BinOper::In, Expr::tuple([Tea::EverydayTea.as_enum()])) | ||
) | ||
.one(db) | ||
.await? | ||
.unwrap() | ||
); | ||
|
||
let res = model.delete(db).await?; | ||
|
||
assert_eq!(res.rows_affected, 1); | ||
assert_eq!(Entity::find().one(db).await?, None); | ||
|
||
Ok(()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh Right
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
is_err
would doThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, good to go