pub struct TableAlterStatement { /* private fields */ }
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"#,
);

Try add a column to an existing table if it does not exists

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

let table = Table::alter()
    .table(Font::Table)
    .add_column_if_not_exists(
        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 IF NOT EXISTS `new_col` int NOT NULL DEFAULT 100"#
);
assert_eq!(
    table.to_string(PostgresQueryBuilder),
    r#"ALTER TABLE "font" ADD COLUMN IF NOT EXISTS "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

Method to call in order to build a Statement

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

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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)

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.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more