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

Construct alter table statement

Set table name

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

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

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

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

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

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.