Struct sea_query::table::TableCreateStatement[][src]

pub struct TableCreateStatement { /* fields omitted */ }
Expand description

Create a table

Examples

use sea_query::{*, tests_cfg::*};

let table = Table::create()
    .table(Char::Table)
    .if_not_exists()
    .col(ColumnDef::new(Char::Id).integer().not_null().auto_increment().primary_key())
    .col(ColumnDef::new(Char::FontSize).integer().not_null())
    .col(ColumnDef::new(Char::Character).string().not_null())
    .col(ColumnDef::new(Char::SizeW).integer().not_null())
    .col(ColumnDef::new(Char::SizeH).integer().not_null())
    .col(ColumnDef::new(Char::FontId).integer().default(Value::Null))
    .foreign_key(
        ForeignKey::create()
            .name("FK_2e303c3a712662f1fc2a4d0aad6")
            .from(Char::Table, Char::FontId)
            .to(Font::Table, Font::Id)
            .on_delete(ForeignKeyAction::Cascade)
            .on_update(ForeignKeyAction::Cascade)
    )
    .to_owned();

assert_eq!(
    table.to_string(MysqlQueryBuilder),
    vec![
        r#"CREATE TABLE IF NOT EXISTS `character` ("#,
            r#"`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,"#,
            r#"`font_size` int NOT NULL,"#,
            r#"`character` varchar(255) NOT NULL,"#,
            r#"`size_w` int NOT NULL,"#,
            r#"`size_h` int NOT NULL,"#,
            r#"`font_id` int DEFAULT NULL,"#,
            r#"CONSTRAINT `FK_2e303c3a712662f1fc2a4d0aad6`"#,
                r#"FOREIGN KEY (`font_id`) REFERENCES `font` (`id`)"#,
                r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
        r#")"#,
    ].join(" ")
);
assert_eq!(
    table.to_string(PostgresQueryBuilder),
    vec![
        r#"CREATE TABLE IF NOT EXISTS "character" ("#,
            r#""id" serial NOT NULL PRIMARY KEY,"#,
            r#""font_size" integer NOT NULL,"#,
            r#""character" varchar NOT NULL,"#,
            r#""size_w" integer NOT NULL,"#,
            r#""size_h" integer NOT NULL,"#,
            r#""font_id" integer DEFAULT NULL,"#,
            r#"CONSTRAINT "FK_2e303c3a712662f1fc2a4d0aad6""#,
                r#"FOREIGN KEY ("font_id") REFERENCES "font" ("id")"#,
                r#"ON DELETE CASCADE ON UPDATE CASCADE"#,
        r#")"#,
    ].join(" ")
);
assert_eq!(
    table.to_string(SqliteQueryBuilder),
    vec![
       r#"CREATE TABLE IF NOT EXISTS `character` ("#,
           r#"`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT,"#,
           r#"`font_size` integer NOT NULL,"#,
           r#"`character` text NOT NULL,"#,
           r#"`size_w` integer NOT NULL,"#,
           r#"`size_h` integer NOT NULL,"#,
           r#"`font_id` integer DEFAULT NULL,"#,
           r#"FOREIGN KEY (`font_id`) REFERENCES `font` (`id`) ON DELETE CASCADE ON UPDATE CASCADE"#,
       r#")"#,
    ].join(" ")
);

Implementations

impl TableCreateStatement[src]

pub fn new() -> Self[src]

Construct create table statement

pub fn create_if_not_exists(&mut self) -> &mut Self[src]

👎 Deprecated since 0.9.6:

Please use the [TableCreateStatement::if_not_exists]

pub fn if_not_exists(&mut self) -> &mut Self[src]

Create table if table not exists

pub fn table<T: 'static>(&mut self, table: T) -> &mut Self where
    T: Iden
[src]

Set table name

pub fn col(&mut self, column: ColumnDef) -> &mut Self[src]

Add a new table column

pub fn index(&mut self, index: IndexCreateStatement) -> &mut Self[src]

Add an index. MySQL only.

Examples

use sea_query::{*, tests_cfg::*};

assert_eq!(
    Table::create()
        .table(Glyph::Table)
        .col(ColumnDef::new(Glyph::Id).integer().not_null())
        .index(
            Index::create()
                .unique()
                .name("idx-glyph-id")
                .col(Glyph::Id)
        )
        .to_string(MysqlQueryBuilder),
    vec![
        "CREATE TABLE `glyph` (",
            "`id` int NOT NULL,",
            "UNIQUE KEY `idx-glyph-id` (`id`)",
        ")",
    ].join(" ")
);

pub fn primary_key(&mut self, index: IndexCreateStatement) -> &mut Self[src]

Add an primary key.

Examples

use sea_query::{*, tests_cfg::*};

let mut statement = Table::create();
statement
    .table(Glyph::Table)
    .col(ColumnDef::new(Glyph::Id).integer().not_null())
    .col(ColumnDef::new(Glyph::Image).string().not_null())
    .primary_key(
        Index::create()
            .col(Glyph::Id)
            .col(Glyph::Image)
    );
assert_eq!(statement.to_string(MysqlQueryBuilder),
    vec![
        "CREATE TABLE `glyph` (",
            "`id` int NOT NULL,",
            "`image` varchar(255) NOT NULL,",
            "PRIMARY KEY (`id`, `image`)",
        ")",
    ].join(" ")
);
assert_eq!(statement.to_string(PostgresQueryBuilder),
    vec![
        "CREATE TABLE \"glyph\" (",
            "\"id\" integer NOT NULL,",
            "\"image\" varchar NOT NULL,",
            "PRIMARY KEY (\"id\", \"image\")",
        ")",
    ].join(" ")
);
assert_eq!(statement.to_string(SqliteQueryBuilder),
    vec![
        "CREATE TABLE `glyph` (",
            "`id` integer NOT NULL,",
            "`image` text NOT NULL,",
            "PRIMARY KEY (`id`, `image`)",
        ")",
    ].join(" ")
);

pub fn foreign_key(
    &mut self,
    foreign_key: ForeignKeyCreateStatement
) -> &mut Self
[src]

Add a foreign key

pub fn engine(&mut self, string: &str) -> &mut Self[src]

Set database engine. MySQL only.

pub fn collate(&mut self, string: &str) -> &mut Self[src]

Set database collate. MySQL only.

pub fn character_set(&mut self, string: &str) -> &mut Self[src]

Set database character set. MySQL only.

impl TableCreateStatement[src]

pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String[src]

Trait Implementations

impl Clone for TableCreateStatement[src]

fn clone(&self) -> TableCreateStatement[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl Debug for TableCreateStatement[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Default for TableCreateStatement[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

impl SchemaStatementBuilder for TableCreateStatement[src]

fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

Build corresponding SQL statement for certain database backend and return SQL string

fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String[src]

Build corresponding SQL statement for certain database backend and return SQL string

fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String[src]

Build corresponding SQL statement for certain database backend and return SQL string

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V