Skip to content

Commit 7ce941b

Browse files
tyt2y3billy1624
andcommitted
Insert Default - Inserting ActiveModel with all attributes NotSet (#432)
* feat: apply alias on `ColumnRef::SchemaTableColumn` * build: update sea-query dependency * feat: insert default * Use sea-query 0.21 Co-authored-by: Billy Chan <[email protected]>
1 parent 4373ab4 commit 7ce941b

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use sea_orm::entity::prelude::*;
2+
3+
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
4+
#[sea_orm(table_name = "insert_default")]
5+
pub struct Model {
6+
#[sea_orm(primary_key)]
7+
pub id: i32,
8+
}
9+
10+
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
11+
pub enum Relation {}
12+
13+
impl ActiveModelBehavior for ActiveModel {}

tests/common/features/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod active_enum;
22
pub mod active_enum_child;
33
pub mod applog;
44
pub mod byte_primary_key;
5+
pub mod insert_default;
56
pub mod metadata;
67
pub mod repository;
78
pub mod satellite;
@@ -13,6 +14,7 @@ pub use active_enum::Entity as ActiveEnum;
1314
pub use active_enum_child::Entity as ActiveEnumChild;
1415
pub use applog::Entity as Applog;
1516
pub use byte_primary_key::Entity as BytePrimaryKey;
17+
pub use insert_default::Entity as InsertDefault;
1618
pub use metadata::Entity as Metadata;
1719
pub use repository::Entity as Repository;
1820
pub use satellite::Entity as Satellite;

tests/common/features/schema.rs

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
3737

3838
create_active_enum_table(db).await?;
3939
create_active_enum_child_table(db).await?;
40+
create_insert_default_table(db).await?;
4041

4142
Ok(())
4243
}
@@ -234,3 +235,18 @@ pub async fn create_satellites_table(db: &DbConn) -> Result<ExecResult, DbErr> {
234235

235236
create_table(db, &stmt, Satellite).await
236237
}
238+
239+
pub async fn create_insert_default_table(db: &DbConn) -> Result<ExecResult, DbErr> {
240+
let create_table_stmt = sea_query::Table::create()
241+
.table(insert_default::Entity.table_ref())
242+
.col(
243+
ColumnDef::new(insert_default::Column::Id)
244+
.integer()
245+
.not_null()
246+
.auto_increment()
247+
.primary_key(),
248+
)
249+
.to_owned();
250+
251+
create_table(db, &create_table_stmt, InsertDefault).await
252+
}

tests/insert_default_tests.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub mod common;
2+
3+
pub use common::{features::*, setup::*, TestContext};
4+
use pretty_assertions::assert_eq;
5+
use sea_orm::entity::prelude::*;
6+
7+
#[sea_orm_macros::test]
8+
#[cfg(any(
9+
feature = "sqlx-mysql",
10+
feature = "sqlx-sqlite",
11+
feature = "sqlx-postgres"
12+
))]
13+
async fn main() -> Result<(), DbErr> {
14+
let ctx = TestContext::new("insert_default_tests").await;
15+
create_tables(&ctx.db).await?;
16+
create_insert_default(&ctx.db).await?;
17+
ctx.delete().await;
18+
19+
Ok(())
20+
}
21+
22+
pub async fn create_insert_default(db: &DatabaseConnection) -> Result<(), DbErr> {
23+
use insert_default::*;
24+
25+
let active_model = ActiveModel {
26+
..Default::default()
27+
};
28+
29+
active_model.clone().insert(db).await?;
30+
active_model.clone().insert(db).await?;
31+
active_model.insert(db).await?;
32+
33+
assert_eq!(
34+
Entity::find().all(db).await?,
35+
vec![Model { id: 1 }, Model { id: 2 }, Model { id: 3 }]
36+
);
37+
38+
Ok(())
39+
}

0 commit comments

Comments
 (0)