Insert Default (#589)
* Insert default * Update sea-query * Fixup Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
This commit is contained in:
parent
c8851646e8
commit
cdc70f4fd9
@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
## Pending
|
## 0.8.0 - Pending
|
||||||
|
|
||||||
### New Features
|
### New Features
|
||||||
* [sea-orm-cli] `sea migrate generate` to generate a new, empty migration file https://github.com/SeaQL/sea-orm/pull/656
|
* [sea-orm-cli] `sea migrate generate` to generate a new, empty migration file https://github.com/SeaQL/sea-orm/pull/656
|
||||||
|
@ -31,7 +31,7 @@ futures-util = { version = "^0.3" }
|
|||||||
tracing = { version = "0.1", features = ["log"] }
|
tracing = { version = "0.1", features = ["log"] }
|
||||||
rust_decimal = { version = "^1", optional = true }
|
rust_decimal = { version = "^1", optional = true }
|
||||||
sea-orm-macros = { version = "^0.7.0", path = "sea-orm-macros", optional = true }
|
sea-orm-macros = { version = "^0.7.0", path = "sea-orm-macros", optional = true }
|
||||||
sea-query = { version = "^0.24.4", features = ["thread-safe"] }
|
sea-query = { version = "^0.24.5", features = ["thread-safe"] }
|
||||||
sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] }
|
sea-strum = { version = "^0.23", features = ["derive", "sea-orm"] }
|
||||||
serde = { version = "^1.0", features = ["derive"] }
|
serde = { version = "^1.0", features = ["derive"] }
|
||||||
serde_json = { version = "^1", optional = true }
|
serde_json = { version = "^1", optional = true }
|
||||||
|
@ -34,6 +34,7 @@ where
|
|||||||
Self {
|
Self {
|
||||||
query: InsertStatement::new()
|
query: InsertStatement::new()
|
||||||
.into_table(A::Entity::default().table_ref())
|
.into_table(A::Entity::default().table_ref())
|
||||||
|
.or_default_values()
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
columns: Vec::new(),
|
columns: Vec::new(),
|
||||||
primary_key: None,
|
primary_key: None,
|
||||||
|
13
tests/common/features/insert_default.rs
Normal file
13
tests/common/features/insert_default.rs
Normal file
@ -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,6 +2,7 @@ pub mod active_enum;
|
|||||||
pub mod active_enum_child;
|
pub mod active_enum_child;
|
||||||
pub mod applog;
|
pub mod applog;
|
||||||
pub mod byte_primary_key;
|
pub mod byte_primary_key;
|
||||||
|
pub mod insert_default;
|
||||||
pub mod metadata;
|
pub mod metadata;
|
||||||
pub mod repository;
|
pub mod repository;
|
||||||
pub mod satellite;
|
pub mod satellite;
|
||||||
@ -14,6 +15,7 @@ pub use active_enum::Entity as ActiveEnum;
|
|||||||
pub use active_enum_child::Entity as ActiveEnumChild;
|
pub use active_enum_child::Entity as ActiveEnumChild;
|
||||||
pub use applog::Entity as Applog;
|
pub use applog::Entity as Applog;
|
||||||
pub use byte_primary_key::Entity as BytePrimaryKey;
|
pub use byte_primary_key::Entity as BytePrimaryKey;
|
||||||
|
pub use insert_default::Entity as InsertDefault;
|
||||||
pub use metadata::Entity as Metadata;
|
pub use metadata::Entity as Metadata;
|
||||||
pub use repository::Entity as Repository;
|
pub use repository::Entity as Repository;
|
||||||
pub use satellite::Entity as Satellite;
|
pub use satellite::Entity as Satellite;
|
||||||
|
@ -38,6 +38,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
|
|
||||||
create_active_enum_table(db).await?;
|
create_active_enum_table(db).await?;
|
||||||
create_active_enum_child_table(db).await?;
|
create_active_enum_child_table(db).await?;
|
||||||
|
create_insert_default_table(db).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -270,3 +271,18 @@ pub async fn create_transaction_log_table(db: &DbConn) -> Result<ExecResult, DbE
|
|||||||
|
|
||||||
create_table(db, &stmt, TransactionLog).await
|
create_table(db, &stmt, TransactionLog).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
tests/insert_default_tests.rs
Normal file
39
tests/insert_default_tests.rs
Normal file
@ -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(())
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user