Struct sea_query::table::TableCreateStatement
source · [−]pub struct TableCreateStatement { /* private fields */ }
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::Int(None)))
.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
sourceimpl TableCreateStatement
impl TableCreateStatement
pub fn create_if_not_exists(&mut self) -> &mut Self
Please use the [TableCreateStatement::if_not_exists
]
sourcepub fn if_not_exists(&mut self) -> &mut Self
pub fn if_not_exists(&mut self) -> &mut Self
Create table if table not exists
sourcepub fn table<T>(&mut self, table: T) -> &mut Self where
T: IntoTableRef,
pub fn table<T>(&mut self, table: T) -> &mut Self where
T: IntoTableRef,
Set table name
sourcepub fn index(&mut self, index: &mut IndexCreateStatement) -> &mut Self
pub fn index(&mut self, index: &mut IndexCreateStatement) -> &mut Self
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(" ")
);
sourcepub fn primary_key(&mut self, index: &mut IndexCreateStatement) -> &mut Self
pub fn primary_key(&mut self, index: &mut IndexCreateStatement) -> &mut Self
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![
r#"CREATE TABLE "glyph" ("#,
r#""id" integer NOT NULL,"#,
r#""image" text NOT NULL,"#,
r#"PRIMARY KEY ("id", "image")"#,
r#")"#,
]
.join(" ")
);
sourcepub fn foreign_key(
&mut self,
foreign_key: &mut ForeignKeyCreateStatement
) -> &mut Self
pub fn foreign_key(
&mut self,
foreign_key: &mut ForeignKeyCreateStatement
) -> &mut Self
Add a foreign key
sourcepub fn character_set(&mut self, string: &str) -> &mut Self
pub fn character_set(&mut self, string: &str) -> &mut Self
Set database character set. MySQL only.
pub fn get_table_name(&self) -> Option<&TableRef>
pub fn get_columns(&self) -> &Vec<ColumnDef>
pub fn get_foreign_key_create_stmts(&self) -> &Vec<ForeignKeyCreateStatement>
pub fn get_indexes(&self) -> &Vec<IndexCreateStatement>
pub fn take(&mut self) -> Self
sourceimpl TableCreateStatement
impl TableCreateStatement
pub fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String
pub fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String
pub fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String
Trait Implementations
sourceimpl Clone for TableCreateStatement
impl Clone for TableCreateStatement
sourcefn clone(&self) -> TableCreateStatement
fn clone(&self) -> TableCreateStatement
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for TableCreateStatement
impl Debug for TableCreateStatement
sourceimpl Default for TableCreateStatement
impl Default for TableCreateStatement
sourceimpl SchemaStatementBuilder for TableCreateStatement
impl SchemaStatementBuilder for TableCreateStatement
sourcefn build<T: SchemaBuilder>(&self, schema_builder: T) -> String
fn build<T: SchemaBuilder>(&self, schema_builder: T) -> String
Build corresponding SQL statement for certain database backend and return SQL string
sourcefn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String
fn build_any(&self, schema_builder: &dyn SchemaBuilder) -> String
Build corresponding SQL statement for certain database backend and return SQL string
sourcefn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String
fn to_string<T: SchemaBuilder>(&self, schema_builder: T) -> String
Build corresponding SQL statement for certain database backend and return SQL string
Auto Trait Implementations
impl !RefUnwindSafe for TableCreateStatement
impl Send for TableCreateStatement
impl Sync for TableCreateStatement
impl Unpin for TableCreateStatement
impl !UnwindSafe for TableCreateStatement
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more