Struct sea_query::table::TableAlterStatement [−][src]
pub struct TableAlterStatement { /* fields omitted */ }
Expand description
Alter a table
Examples
use sea_query::{*, tests_cfg::*};
let table = Table::alter()
.table(Font::Table)
.add_column(ColumnDef::new(Alias::new("new_col")).integer().not_null().default(100))
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
r#"ALTER TABLE `font` ADD COLUMN `new_col` int NOT NULL DEFAULT 100"#
);
assert_eq!(
table.to_string(PostgresQueryBuilder),
r#"ALTER TABLE "font" ADD COLUMN "new_col" integer NOT NULL DEFAULT 100"#
);
assert_eq!(
table.to_string(SqliteQueryBuilder),
r#"ALTER TABLE `font` ADD COLUMN `new_col` integer NOT NULL DEFAULT 100"#,
);
Implementations
Add a column to an existing table
Examples
use sea_query::{*, tests_cfg::*};
let table = Table::alter()
.table(Font::Table)
.add_column(ColumnDef::new(Alias::new("new_col")).integer().not_null().default(100))
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
r#"ALTER TABLE `font` ADD COLUMN `new_col` int NOT NULL DEFAULT 100"#
);
assert_eq!(
table.to_string(PostgresQueryBuilder),
r#"ALTER TABLE "font" ADD COLUMN "new_col" integer NOT NULL DEFAULT 100"#
);
assert_eq!(
table.to_string(SqliteQueryBuilder),
r#"ALTER TABLE `font` ADD COLUMN `new_col` integer NOT NULL DEFAULT 100"#,
);
Modify a column in an existing table
Examples
use sea_query::{*, tests_cfg::*};
let table = Table::alter()
.table(Font::Table)
.modify_column(ColumnDef::new(Alias::new("new_col")).big_integer().default(999))
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
r#"ALTER TABLE `font` MODIFY COLUMN `new_col` bigint DEFAULT 999"#
);
assert_eq!(
table.to_string(PostgresQueryBuilder),
vec![
r#"ALTER TABLE "font""#,
r#"ALTER COLUMN "new_col" TYPE bigint,"#,
r#"ALTER COLUMN "new_col" SET DEFAULT 999"#,
].join(" ")
);
// Sqlite not support modifying table column
pub fn rename_column<T: 'static, R: 'static>(
&mut self,
from_name: T,
to_name: R
) -> &mut Self where
T: Iden,
R: Iden,
pub fn rename_column<T: 'static, R: 'static>(
&mut self,
from_name: T,
to_name: R
) -> &mut Self where
T: Iden,
R: Iden,
Rename a column in an existing table
Examples
use sea_query::{*, tests_cfg::*};
let table = Table::alter()
.table(Font::Table)
.rename_column(Alias::new("new_col"), Alias::new("new_column"))
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
r#"ALTER TABLE `font` RENAME COLUMN `new_col` TO `new_column`"#
);
assert_eq!(
table.to_string(PostgresQueryBuilder),
r#"ALTER TABLE "font" RENAME COLUMN "new_col" TO "new_column""#
);
assert_eq!(
table.to_string(SqliteQueryBuilder),
r#"ALTER TABLE `font` RENAME COLUMN `new_col` TO `new_column`"#
);
Add a column to existing table
Examples
use sea_query::{*, tests_cfg::*};
let table = Table::alter()
.table(Font::Table)
.drop_column(Alias::new("new_column"))
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
r#"ALTER TABLE `font` DROP COLUMN `new_column`"#
);
assert_eq!(
table.to_string(PostgresQueryBuilder),
r#"ALTER TABLE "font" DROP COLUMN "new_column""#
);
// Sqlite not support modifying table column
Trait Implementations
Build corresponding SQL statement for certain database backend and return SQL string
Build corresponding SQL statement for certain database backend and return SQL string
Build corresponding SQL statement for certain database backend and return SQL string
Auto Trait Implementations
impl !RefUnwindSafe for TableAlterStatement
impl Send for TableAlterStatement
impl Sync for TableAlterStatement
impl Unpin for TableAlterStatement
impl !UnwindSafe for TableAlterStatement
Blanket Implementations
Mutably borrows from an owned value. Read more
type Output = T
type Output = T
Should always be Self