Skip to content

Commit

Permalink
Merge pull request #318 from josh-codes/sea-orm-table-ref
Browse files Browse the repository at this point in the history
Pass the argument `entity.table_ref()` instead of just `entity`.
  • Loading branch information
tyt2y3 authored Nov 17, 2021
2 parents f2a7745 + a8ff893 commit 52bd28b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 7 deletions.
96 changes: 93 additions & 3 deletions src/schema/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ where
);
}

stmt.table(entity).take()
stmt.table(entity.table_ref()).take()
}

#[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
Expand Down Expand Up @@ -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)
);
}
}
2 changes: 1 addition & 1 deletion tests/common/features/active_enum.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
5 changes: 3 additions & 2 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -146,7 +147,7 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr>
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()
Expand Down
2 changes: 1 addition & 1 deletion tests/common/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down

0 comments on commit 52bd28b

Please sign in to comment.