Skip to main content

morph_columns

Function morph_columns 

Source
pub fn morph_columns(name: &str, key: KeyType) -> [ColumnDef; 2]
Expand description

The {name}_type and {name}_id columns, NOT NULL.

use sea_orm::sea_query::{PostgresQueryBuilder, Table};
use sea_orm_relations_kit::schema::{morph_columns, morph_index, KeyType};

// In a SeaORM migration, pass these to `manager.create_table(...)` and
// `manager.create_index(...)`.
let [type_column, id_column] = morph_columns("commentable", KeyType::BigInteger);
let table = Table::create()
    .table("comments")
    .col(type_column)
    .col(id_column)
    .to_owned();

assert_eq!(
    table.to_string(PostgresQueryBuilder),
    r#"CREATE TABLE "comments" ( "commentable_type" varchar(255) NOT NULL, "commentable_id" bigint NOT NULL )"#
);
assert_eq!(
    morph_index("comments", "commentable").to_string(PostgresQueryBuilder),
    r#"CREATE INDEX "comments_commentable_type_commentable_id_index" ON "comments" ("commentable_type", "commentable_id")"#
);