Struct ColumnDef

Source
pub struct ColumnDef {
    pub(crate) table: Option<TableRef>,
    pub(crate) name: DynIden,
    pub(crate) types: Option<ColumnType>,
    pub(crate) spec: Vec<ColumnSpec>,
}
Expand description

Specification of a table column

Fields§

§table: Option<TableRef>§name: DynIden§types: Option<ColumnType>§spec: Vec<ColumnSpec>

Implementations§

Source§

impl ColumnDef

Source

pub fn new<T>(name: T) -> Self
where T: IntoIden,

Construct a table column

Source

pub fn new_with_type<T>(name: T, types: ColumnType) -> Self
where T: IntoIden,

Construct a table column with column type

Source

pub fn not_null(&mut self) -> &mut Self

Set column not null

Source

pub fn null(&mut self) -> &mut Self

Set column null

Source

pub fn default<T>(&mut self, value: T) -> &mut Self
where T: Into<Expr>,

Set default expression of a column

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

let table = Table::create()
    .table(Char::Table)
    .col(ColumnDef::new(Char::FontId).integer().default(12i32))
    .col(
        ColumnDef::new(Char::CreatedAt)
            .timestamp()
            .default(Expr::current_timestamp())
            .not_null(),
    )
    .to_owned();

assert_eq!(
    table.to_string(MysqlQueryBuilder),
    [
        "CREATE TABLE `character` (",
        "`font_id` int DEFAULT 12,",
        "`created_at` timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL",
        ")",
    ]
    .join(" ")
);

assert_eq!(
    table.to_string(PostgresQueryBuilder),
    [
        r#"CREATE TABLE "character" ("#,
        r#""font_id" integer DEFAULT 12,"#,
        r#""created_at" timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL"#,
        r#")"#,
    ]
    .join(" ")
);
Source

pub fn auto_increment(&mut self) -> &mut Self

Set column auto increment

Source

pub fn unique_key(&mut self) -> &mut Self

Set column unique constraint

Source

pub fn primary_key(&mut self) -> &mut Self

Set column as primary key

Source

pub fn char_len(&mut self, length: u32) -> &mut Self

Set column type as char with custom length

Source

pub fn char(&mut self) -> &mut Self

Set column type as char

Source

pub fn string_len(&mut self, length: u32) -> &mut Self

Set column type as string with custom length

Source

pub fn string(&mut self) -> &mut Self

Set column type as string

Source

pub fn text(&mut self) -> &mut Self

Set column type as text

Source

pub fn tiny_integer(&mut self) -> &mut Self

Set column type as tiny_integer

Source

pub fn small_integer(&mut self) -> &mut Self

Set column type as small_integer

Source

pub fn integer(&mut self) -> &mut Self

Set column type as integer

Source

pub fn big_integer(&mut self) -> &mut Self

Set column type as big_integer

Source

pub fn tiny_unsigned(&mut self) -> &mut Self

Set column type as tiny_unsigned

Source

pub fn small_unsigned(&mut self) -> &mut Self

Set column type as small_unsigned

Source

pub fn unsigned(&mut self) -> &mut Self

Set column type as unsigned

Source

pub fn big_unsigned(&mut self) -> &mut Self

Set column type as big_unsigned

Source

pub fn float(&mut self) -> &mut Self

Set column type as float

Source

pub fn double(&mut self) -> &mut Self

Set column type as double

Source

pub fn decimal_len(&mut self, precision: u32, scale: u32) -> &mut Self

Set column type as decimal with custom precision and scale

Source

pub fn decimal(&mut self) -> &mut Self

Set column type as decimal

Source

pub fn date_time(&mut self) -> &mut Self

Set column type as date_time

Source

pub fn timestamp(&mut self) -> &mut Self

Set column type as timestamp

Source

pub fn timestamp_with_time_zone(&mut self) -> &mut Self

Set column type as timestamp with time zone. Postgres only

Source

pub fn time(&mut self) -> &mut Self

Set column type as time

Source

pub fn date(&mut self) -> &mut Self

Set column type as date

Source

pub fn year(&mut self) -> &mut Self

Set column type as year Only MySQL supports year

Source

pub fn binary_len(&mut self, length: u32) -> &mut Self

Set column type as binary with custom length

Source

pub fn binary(&mut self) -> &mut Self

Set column type as binary with default length of 1

Source

pub fn var_binary(&mut self, length: u32) -> &mut Self

Set column type as binary with variable length

Source

pub fn bit(&mut self, length: Option<u32>) -> &mut Self

Set column type as bit with variable length

Source

pub fn varbit(&mut self, length: u32) -> &mut Self

Set column type as varbit with variable length

Source

pub fn blob(&mut self) -> &mut Self

Set column type as blob

Source

pub fn boolean(&mut self) -> &mut Self

Set column type as boolean

Source

pub fn money_len(&mut self, precision: u32, scale: u32) -> &mut Self

Set column type as money with custom precision and scale

Source

pub fn money(&mut self) -> &mut Self

Set column type as money

Source

pub fn json(&mut self) -> &mut Self

Set column type as json.

Source

pub fn json_binary(&mut self) -> &mut Self

Set column type as json binary.

Source

pub fn uuid(&mut self) -> &mut Self

Set column type as uuid

Source

pub fn custom<T>(&mut self, name: T) -> &mut Self
where T: IntoIden,

Use a custom type on this column.

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

let table = Table::create()
    .table(Char::Table)
    .col(
        ColumnDef::new(Char::Id)
            .custom("new_type")
            .not_null()
            .primary_key(),
    )
    .to_owned();

assert_eq!(
    table.to_string(MysqlQueryBuilder),
    [
        r#"CREATE TABLE `character` ("#,
        r#"`id` new_type NOT NULL PRIMARY KEY"#,
        r#")"#,
    ]
    .join(" ")
);
assert_eq!(
    table.to_string(PostgresQueryBuilder),
    [
        r#"CREATE TABLE "character" ("#,
        r#""id" new_type NOT NULL PRIMARY KEY"#,
        r#")"#,
    ]
    .join(" ")
);
assert_eq!(
    table.to_string(SqliteQueryBuilder),
    [
        r#"CREATE TABLE "character" ("#,
        r#""id" new_type NOT NULL PRIMARY KEY"#,
        r#")"#,
    ]
    .join(" ")
);
Source

pub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Self
where N: IntoIden, S: IntoIden, V: IntoIterator<Item = S>,

Set column type as enum.

Source

pub fn array(&mut self, elem_type: ColumnType) -> &mut Self

Set column type as an array with a specified element type. This is only supported on Postgres.

Source

pub fn cidr(&mut self) -> &mut Self

Set columnt type as cidr. This is only supported on Postgres.

Source

pub fn inet(&mut self) -> &mut Self

Set columnt type as inet. This is only supported on Postgres.

Source

pub fn mac_address(&mut self) -> &mut Self

Set columnt type as macaddr. This is only supported on Postgres.

Source

pub fn ltree(&mut self) -> &mut Self

Set column type as ltree This is only supported on Postgres.

use sea_query::{tests_cfg::*, *};
assert_eq!(
    Table::create()
        .table(Glyph::Table)
        .col(
            ColumnDef::new(Glyph::Id)
                .integer()
                .not_null()
                .auto_increment()
                .primary_key()
        )
        .col(ColumnDef::new(Glyph::Tokens).ltree())
        .to_string(PostgresQueryBuilder),
    [
        r#"CREATE TABLE "glyph" ("#,
        r#""id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,"#,
        r#""tokens" ltree"#,
        r#")"#,
    ]
    .join(" ")
);
Source

pub fn check<T>(&mut self, check: T) -> &mut Self
where T: Into<Check>,

Set constraints as Expr

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

assert_eq!(
    Table::create()
        .table(Glyph::Table)
        .col(
            ColumnDef::new(Glyph::Id)
                .integer()
                .not_null()
                .check(Expr::col(Glyph::Id).gt(10))
        )
        .to_string(MysqlQueryBuilder),
    r#"CREATE TABLE `glyph` ( `id` int NOT NULL CHECK (`id` > 10) )"#,
);

assert_eq!(
    Table::create()
        .table(Glyph::Table)
        .col(
            ColumnDef::new(Glyph::Id)
                .integer()
                .not_null()
                .check(("positive_id", Expr::col(Glyph::Id).gt(10)))
        )
        .to_string(MysqlQueryBuilder),
    r#"CREATE TABLE `glyph` ( `id` int NOT NULL CONSTRAINT `positive_id` CHECK (`id` > 10) )"#,
);
Source

pub fn generated<T>(&mut self, expr: T, stored: bool) -> &mut Self
where T: Into<Expr>,

Sets the column as generated with Expr

Source

pub fn extra<T>(&mut self, string: T) -> &mut Self
where T: Into<String>,

Some extra options in custom string

use sea_query::{tests_cfg::*, *};
let table = Table::create()
    .table(Char::Table)
    .col(
        ColumnDef::new(Char::Id)
            .uuid()
            .extra("DEFAULT gen_random_uuid()")
            .primary_key()
            .not_null(),
    )
    .col(
        ColumnDef::new(Char::CreatedAt)
            .timestamp_with_time_zone()
            .extra("DEFAULT NOW()")
            .not_null(),
    )
    .to_owned();
assert_eq!(
    table.to_string(PostgresQueryBuilder),
    [
        r#"CREATE TABLE "character" ("#,
        r#""id" uuid DEFAULT gen_random_uuid() PRIMARY KEY NOT NULL,"#,
        r#""created_at" timestamp with time zone DEFAULT NOW() NOT NULL"#,
        r#")"#,
    ]
    .join(" ")
);
Source

pub fn using<T>(&mut self, value: T) -> &mut Self
where T: Into<Expr>,

Some extra options in custom string

use sea_query::{tests_cfg::*, *};
let table = Table::alter()
    .table(Char::Table)
    .modify_column(
        ColumnDef::new(Char::Id)
            .integer()
            .using(Expr::col(Char::Id).cast_as("integer")),
    )
    .to_owned();
assert_eq!(
    table.to_string(PostgresQueryBuilder),
    [
        r#"ALTER TABLE "character""#,
        r#"ALTER COLUMN "id" TYPE integer USING CAST("id" AS integer)"#,
    ]
    .join(" ")
);
Source

pub fn comment<T>(&mut self, string: T) -> &mut Self
where T: Into<String>,

MySQL only.

Source

pub fn get_column_name(&self) -> String

Source

pub fn get_column_type(&self) -> Option<&ColumnType>

Source

pub fn get_column_spec(&self) -> &Vec<ColumnSpec>

Source

pub fn take(&mut self) -> Self

Trait Implementations§

Source§

impl Clone for ColumnDef

Source§

fn clone(&self) -> ColumnDef

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ColumnDef

Source§

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

Formats the value using the given formatter. Read more
Source§

impl From<&mut ColumnDef> for ColumnDef

Source§

fn from(col: &mut ColumnDef) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoColumnDef for T
where T: Into<ColumnDef>,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.