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

Insert Default - Inserting ActiveModel with all attributes NotSet #432

Merged
merged 5 commits into from
Feb 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions build-tools/cargo-publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ git commit -am "sea-orm-codegen $1"
cargo publish
cd ..
sleep 30
cd sea-orm-cli
sed -i 's/^version.*$/'"version = \"$1\"/" Cargo.toml
sed -i 's/^sea-orm-codegen [^,]*,/sea-orm-codegen = { version = "\^'$1'",/' Cargo.toml
git commit -am "sea-orm-cli $1"
cargo publish
cd ..
sleep 30
# cd sea-orm-cli
# sed -i 's/^version.*$/'"version = \"$1\"/" Cargo.toml
# sed -i 's/^sea-orm-codegen [^,]*,/sea-orm-codegen = { version = "\^'$1'",/' Cargo.toml
# git commit -am "sea-orm-cli $1"
# cargo publish
# cd ..
# sleep 30
cd sea-orm-macros
sed -i 's/^version.*$/version = "'$1'"/' Cargo.toml
git commit -am "sea-orm-macros $1"
Expand Down
13 changes: 13 additions & 0 deletions tests/common/features/insert_default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "insert_default")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
}

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

impl ActiveModelBehavior for ActiveModel {}
2 changes: 2 additions & 0 deletions tests/common/features/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod active_enum;
pub mod active_enum_child;
pub mod applog;
pub mod byte_primary_key;
pub mod insert_default;
pub mod metadata;
pub mod repository;
pub mod satellite;
Expand All @@ -13,6 +14,7 @@ pub use active_enum::Entity as ActiveEnum;
pub use active_enum_child::Entity as ActiveEnumChild;
pub use applog::Entity as Applog;
pub use byte_primary_key::Entity as BytePrimaryKey;
pub use insert_default::Entity as InsertDefault;
pub use metadata::Entity as Metadata;
pub use repository::Entity as Repository;
pub use satellite::Entity as Satellite;
Expand Down
16 changes: 16 additions & 0 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {

create_active_enum_table(db).await?;
create_active_enum_child_table(db).await?;
create_insert_default_table(db).await?;

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

create_table(db, &stmt, Satellite).await
}

pub async fn create_insert_default_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let create_table_stmt = sea_query::Table::create()
.table(insert_default::Entity.table_ref())
.col(
ColumnDef::new(insert_default::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.to_owned();

create_table(db, &create_table_stmt, InsertDefault).await
}
39 changes: 39 additions & 0 deletions tests/insert_default_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub mod common;

pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::entity::prelude::*;

#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("insert_default_tests").await;
create_tables(&ctx.db).await?;
create_insert_default(&ctx.db).await?;
ctx.delete().await;

Ok(())
}

pub async fn create_insert_default(db: &DatabaseConnection) -> Result<(), DbErr> {
use insert_default::*;

let active_model = ActiveModel {
..Default::default()
};

active_model.clone().insert(db).await?;
active_model.clone().insert(db).await?;
active_model.insert(db).await?;

assert_eq!(
Entity::find().all(db).await?,
vec![Model { id: 1 }, Model { id: 2 }, Model { id: 3 }]
);

Ok(())
}