-
-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
ActiveEnum
field as composite primary key (#1414)
* Use ActiveEnum field as part of composite primary key * Try unique constraint failed
- Loading branch information
Showing
5 changed files
with
173 additions
and
1 deletion.
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,126 @@ | ||
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() | ||
); | ||
|
||
// UNIQUE constraint failed | ||
assert!(ActiveModel { | ||
id: Set(Tea::EverydayTea), | ||
category: Set(Some(Category::Big)), | ||
color: Set(Some(Color::Black)), | ||
..Default::default() | ||
} | ||
.insert(db) | ||
.await | ||
.is_err()); | ||
|
||
// UNIQUE constraint failed | ||
assert!(Entity::insert(ActiveModel { | ||
id: Set(Tea::EverydayTea), | ||
category: Set(Some(Category::Big)), | ||
color: Set(Some(Color::Black)), | ||
..Default::default() | ||
}) | ||
.exec(db) | ||
.await | ||
.is_err()); | ||
|
||
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(()) | ||
} |