From 2b87a8ba32ff1b05b50209de2729b7b77e859b93 Mon Sep 17 00:00:00 2001 From: Josh <43808099+josh-codes@users.noreply.github.com> Date: Mon, 15 Nov 2021 17:10:02 +0800 Subject: [PATCH 1/8] Pass the argument `entity.table_ref()` instead of just `entity`. --- src/schema/entity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/schema/entity.rs b/src/schema/entity.rs index 8b0ad1444..58b168de6 100644 --- a/src/schema/entity.rs +++ b/src/schema/entity.rs @@ -165,7 +165,7 @@ where ); } - stmt.table(entity).take() + stmt.table(entity.table_ref()).take() } #[cfg(test)] From 31fae07bc1617796b004e29c35f6f9a7ba2e61b9 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 16 Nov 2021 12:04:06 +0800 Subject: [PATCH 2/8] Use "josh-codes/sea-query" --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2345844cd..da863fee1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ futures-util = { version = "^0.3" } log = { version = "^0.4", optional = true } rust_decimal = { version = "^1", optional = true } sea-orm-macros = { version = "^0.3.1", path = "sea-orm-macros", optional = true } -sea-query = { version = "^0.18.0", git = "https://github.com/SeaQL/sea-query.git", features = ["thread-safe"] } +sea-query = { version = "^0.18.0", git = "https://github.com/josh-codes/sea-query.git", branch = "sea-query-change-type-into-table-ref", features = ["thread-safe"] } sea-strum = { version = "^0.21", features = ["derive", "sea-orm"] } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1", optional = true } From 36886e74cd1dd80024fb9f14c97ed38de87ced6f Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 16 Nov 2021 12:04:21 +0800 Subject: [PATCH 3/8] Testing & fixup --- src/schema/entity.rs | 94 +++++++++++++++++++++++++++- tests/common/features/active_enum.rs | 2 +- tests/common/features/schema.rs | 5 +- 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/src/schema/entity.rs b/src/schema/entity.rs index 58b168de6..a78625cca 100644 --- a/src/schema/entity.rs +++ b/src/schema/entity.rs @@ -170,11 +170,11 @@ where #[cfg(test)] mod tests { - use crate::{sea_query::*, tests_cfg::*, DbBackend, Schema}; + use crate::{sea_query::*, tests_cfg::*, DbBackend, EntityName, Schema}; use pretty_assertions::assert_eq; #[test] - fn test_create_table_from_entity() { + fn test_mysql_create_table_from_entity() { let schema = Schema::new(DbBackend::MySql); assert_eq!( schema @@ -217,4 +217,94 @@ mod tests { .to_string(MysqlQueryBuilder) ); } + + #[test] + fn test_postgres_create_table_from_entity() { + let schema = Schema::new(DbBackend::Postgres); + assert_eq!( + schema + .create_table_from_entity(CakeFillingPrice) + .to_string(PostgresQueryBuilder), + Table::create() + .table(CakeFillingPrice.table_ref()) + .col( + ColumnDef::new(cake_filling_price::Column::CakeId) + .integer() + .not_null() + ) + .col( + ColumnDef::new(cake_filling_price::Column::FillingId) + .integer() + .not_null() + ) + .col( + ColumnDef::new(cake_filling_price::Column::Price) + .decimal() + .not_null() + ) + .primary_key( + Index::create() + .name("pk-cake_filling_price") + .col(cake_filling_price::Column::CakeId) + .col(cake_filling_price::Column::FillingId) + .primary() + ) + .foreign_key( + ForeignKeyCreateStatement::new() + .name("fk-cake_filling_price-cake_filling") + .from_tbl(CakeFillingPrice) + .from_col(cake_filling_price::Column::CakeId) + .from_col(cake_filling_price::Column::FillingId) + .to_tbl(CakeFilling) + .to_col(cake_filling::Column::CakeId) + .to_col(cake_filling::Column::FillingId) + ) + .to_string(PostgresQueryBuilder) + ); + } + + #[test] + fn test_sqlite_create_table_from_entity() { + let schema = Schema::new(DbBackend::Sqlite); + assert_eq!( + schema + .create_table_from_entity(CakeFillingPrice) + .to_string(SqliteQueryBuilder), + Table::create() + .table(CakeFillingPrice) + .col( + ColumnDef::new(cake_filling_price::Column::CakeId) + .integer() + .not_null() + ) + .col( + ColumnDef::new(cake_filling_price::Column::FillingId) + .integer() + .not_null() + ) + .col( + ColumnDef::new(cake_filling_price::Column::Price) + .decimal() + .not_null() + ) + .primary_key( + Index::create() + .name("pk-cake_filling_price") + .col(cake_filling_price::Column::CakeId) + .col(cake_filling_price::Column::FillingId) + .primary() + ) + .foreign_key( + ForeignKeyCreateStatement::new() + .name("fk-cake_filling_price-cake_filling") + .from_tbl(CakeFillingPrice) + .from_col(cake_filling_price::Column::CakeId) + .from_col(cake_filling_price::Column::FillingId) + .to_tbl(CakeFilling) + .to_col(cake_filling::Column::CakeId) + .to_col(cake_filling::Column::FillingId) + ) + .to_string(SqliteQueryBuilder) + ); + } } diff --git a/tests/common/features/active_enum.rs b/tests/common/features/active_enum.rs index 5285c5d94..9df91b45b 100644 --- a/tests/common/features/active_enum.rs +++ b/tests/common/features/active_enum.rs @@ -1,7 +1,7 @@ use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] -#[sea_orm(table_name = "active_enum")] +#[sea_orm(schema_name = "public", table_name = "active_enum")] pub struct Model { #[sea_orm(primary_key)] pub id: i32, diff --git a/tests/common/features/schema.rs b/tests/common/features/schema.rs index 942776047..1504b2852 100644 --- a/tests/common/features/schema.rs +++ b/tests/common/features/schema.rs @@ -3,7 +3,8 @@ pub use super::super::bakery_chain::*; use super::*; use crate::common::setup::{create_enum, create_table, create_table_without_asserts}; use sea_orm::{ - error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, ExecResult, + error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName, + ExecResult, }; use sea_query::{extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement}; @@ -146,7 +147,7 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result DbBackend::Postgres => tea_col.custom(tea_enum), }; let create_table_stmt = sea_query::Table::create() - .table(active_enum::Entity) + .table(active_enum::Entity.table_ref()) .col( ColumnDef::new(active_enum::Column::Id) .integer() From 3d5f4424a8a6ad33f90e9a85aaa224a45b8da629 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 16 Nov 2021 13:09:07 +0800 Subject: [PATCH 4/8] Fixup --- tests/common/setup/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/common/setup/mod.rs b/tests/common/setup/mod.rs index 62e06b654..519be2e37 100644 --- a/tests/common/setup/mod.rs +++ b/tests/common/setup/mod.rs @@ -150,7 +150,7 @@ pub async fn create_table_without_asserts( if builder != DbBackend::Sqlite { let stmt = builder.build( Table::drop() - .table(Alias::new(create.get_table_name().unwrap().as_ref())) + .table(create.get_table_name().unwrap().clone()) .if_exists() .cascade(), ); From bd67927b157707e0357b4c380fa9a5615fa92cbe Mon Sep 17 00:00:00 2001 From: Josh <43808099+josh-codes@users.noreply.github.com> Date: Tue, 16 Nov 2021 10:51:37 +0000 Subject: [PATCH 5/8] Change the version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da863fee1..edfd3b9b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = [".", "sea-orm-macros", "sea-orm-codegen"] [package] name = "sea-orm" -version = "0.3.1" +version = "0.3.3" authors = ["Chris Tsang "] edition = "2021" description = "🐚 An async & dynamic ORM for Rust" From a9de2b94f9a222054f1a3b01677f22d54693c3d6 Mon Sep 17 00:00:00 2001 From: Josh <43808099+josh-codes@users.noreply.github.com> Date: Tue, 16 Nov 2021 10:53:09 +0000 Subject: [PATCH 6/8] Chnage editon from 2021 to 2018 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index edfd3b9b6..f73609f5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ members = [".", "sea-orm-macros", "sea-orm-codegen"] name = "sea-orm" version = "0.3.3" authors = ["Chris Tsang "] -edition = "2021" +edition = "2018" description = "🐚 An async & dynamic ORM for Rust" license = "MIT OR Apache-2.0" documentation = "https://docs.rs/sea-orm" From c25fcb601ed74adf2622bcc1b93515b75dc878ad Mon Sep 17 00:00:00 2001 From: Josh <43808099+josh-codes@users.noreply.github.com> Date: Tue, 16 Nov 2021 10:55:30 +0000 Subject: [PATCH 7/8] Undo changes --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f73609f5e..da863fee1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,9 +3,9 @@ members = [".", "sea-orm-macros", "sea-orm-codegen"] [package] name = "sea-orm" -version = "0.3.3" +version = "0.3.1" authors = ["Chris Tsang "] -edition = "2018" +edition = "2021" description = "🐚 An async & dynamic ORM for Rust" license = "MIT OR Apache-2.0" documentation = "https://docs.rs/sea-orm" From a8ff8935f659bd614ece07cb3faa591e392ed987 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Wed, 17 Nov 2021 15:18:40 +0800 Subject: [PATCH 8/8] Undo sea-query dep changes --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da863fee1..2345844cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,7 @@ futures-util = { version = "^0.3" } log = { version = "^0.4", optional = true } rust_decimal = { version = "^1", optional = true } sea-orm-macros = { version = "^0.3.1", path = "sea-orm-macros", optional = true } -sea-query = { version = "^0.18.0", git = "https://github.com/josh-codes/sea-query.git", branch = "sea-query-change-type-into-table-ref", features = ["thread-safe"] } +sea-query = { version = "^0.18.0", git = "https://github.com/SeaQL/sea-query.git", features = ["thread-safe"] } sea-strum = { version = "^0.21", features = ["derive", "sea-orm"] } serde = { version = "^1.0", features = ["derive"] } serde_json = { version = "^1", optional = true }