sea_query::table

Struct ColumnDef

source
pub struct ColumnDef { /* private fields */ }
Expand description

Specification of a table column

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<SimpleExpr>,

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 interval( &mut self, fields: Option<PgInterval>, precision: Option<u32>, ) -> &mut Self

Set column type as interval type with optional fields and precision. Postgres only

use sea_query::{tests_cfg::*, *};
assert_eq!(
    Table::create()
        .table(Glyph::Table)
        .col(
            ColumnDef::new(Alias::new("I1"))
                .interval(None, None)
                .not_null()
        )
        .col(
            ColumnDef::new(Alias::new("I2"))
                .interval(Some(PgInterval::YearToMonth), None)
                .not_null()
        )
        .col(
            ColumnDef::new(Alias::new("I3"))
                .interval(None, Some(42))
                .not_null()
        )
        .col(
            ColumnDef::new(Alias::new("I4"))
                .interval(Some(PgInterval::Hour), Some(43))
                .not_null()
        )
        .to_string(PostgresQueryBuilder),
    [
        r#"CREATE TABLE "glyph" ("#,
        r#""I1" interval NOT NULL,"#,
        r#""I2" interval YEAR TO MONTH NOT NULL,"#,
        r#""I3" interval(42) NOT NULL,"#,
        r#""I4" interval HOUR(43) NOT NULL"#,
        r#")"#,
    ]
    .join(" ")
);
source

pub fn vector(&mut self, size: Option<u32>) -> &mut Self

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.

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" serial NOT NULL PRIMARY KEY,"#,
        r#""tokens" ltree"#,
        r#")"#,
    ]
    .join(" ")
);
source

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

Set constraints as SimpleExpr

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) )"#,
);
source

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

Sets the column as generated with SimpleExpr

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 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 copy 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 IntoColumnDef for &mut ColumnDef

sourceยง

impl IntoColumnDef for ColumnDef

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, dst: *mut T)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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.