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

Specification of a table column

Implementations§

source§

impl ColumnDef

source

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

Construct a table column

source

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

Construct a table column with column type

source

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

Set column not null

source

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

Set column null

source

pub fn default<T>(&mut self, value: T) -> &mut ColumnDef
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 ColumnDef

Set column auto increment

source

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

Set column unique constraint

source

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

Set column as primary key

source

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

Set column type as char with custom length

source

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

Set column type as char

source

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

Set column type as string with custom length

source

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

Set column type as string

source

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

Set column type as text

source

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

Set column type as tiny_integer

source

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

Set column type as small_integer

source

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

Set column type as integer

source

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

Set column type as big_integer

source

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

Set column type as tiny_unsigned

source

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

Set column type as small_unsigned

source

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

Set column type as unsigned

source

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

Set column type as big_unsigned

source

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

Set column type as float

source

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

Set column type as double

source

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

Set column type as decimal with custom precision and scale

source

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

Set column type as decimal

source

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

Set column type as date_time

source

pub fn interval( &mut self, fields: Option<PgInterval>, precision: Option<u32> ) -> &mut ColumnDef

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 timestamp(&mut self) -> &mut ColumnDef

Set column type as timestamp

source

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

Set column type as timestamp with time zone. Postgres only

source

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

Set column type as time

source

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

Set column type as date

source

pub fn year(&mut self, length: Option<MySqlYear>) -> &mut ColumnDef

Set column type as year Only MySQL supports year

source

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

Set column type as binary with custom length

source

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

Set column type as binary

source

pub fn blob(&mut self, size: BlobSize) -> &mut ColumnDef

Set column type as blob, but when given BlobSize::Blob(size) argument, this column map to binary(size) type instead.

source

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

Set column type as binary with variable length

source

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

Set column type as bit with variable length

source

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

Set column type as varbit with variable length

source

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

Set column type as boolean

source

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

Set column type as money with custom precision and scale

source

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

Set column type as money

source

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

Set column type as json.

source

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

Set column type as json binary.

source

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

Set column type as uuid

source

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

Use a custom type on this column.

source

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

Set column type as enum.

source

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

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

source

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

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

source

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

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

source

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

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

source

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

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 ColumnDef
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 ColumnDef
where T: Into<SimpleExpr>,

Sets the column as generated with SimpleExpr

source

pub fn extra<T>(&mut self, string: T) -> &mut ColumnDef
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 ColumnDef
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) -> ColumnDef

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<(), Error>

Formats the value using the given formatter. Read more

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> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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,

§

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

§

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

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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