Struct ColumnDef
pub struct ColumnDef { /* private fields */ }Expand description
Specification of a table column
Implementations§
§impl ColumnDef
impl ColumnDef
pub fn new_with_type<T>(name: T, types: ColumnType) -> Selfwhere
T: IntoIden,
pub fn new_with_type<T>(name: T, types: ColumnType) -> Selfwhere
T: IntoIden,
Construct a table column with column type
pub fn not_null(&mut self) -> &mut Self
pub fn not_null(&mut self) -> &mut Self
Set column not null
pub fn null(&mut self) -> &mut Self
pub fn null(&mut self) -> &mut Self
Set column null
pub fn default<T>(&mut self, value: T) -> &mut Self
pub fn default<T>(&mut self, value: T) -> &mut Self
Set default expression of a column
use sea_query::{tests_cfg::*, *};
let table = Table::create()
.table("character")
.col(ColumnDef::new("font_id").integer().default(12i32))
.col(
ColumnDef::new("font_id_2")
.integer()
.default(Expr::val(12).add(2)),
)
.col(
ColumnDef::new("created_at")
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned();
assert_eq!(
table.to_string(MysqlQueryBuilder),
[
"CREATE TABLE `character` (",
"`font_id` int DEFAULT 12,",
"`font_id_2` int DEFAULT (12 + 2),",
"`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
")",
]
.join(" ")
);
assert_eq!(
table.to_string(PostgresQueryBuilder),
[
r#"CREATE TABLE "character" ("#,
r#""font_id" integer DEFAULT 12,"#,
r#""font_id_2" integer DEFAULT (12 + 2),"#,
r#""created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"#,
r#")"#,
]
.join(" ")
);pub fn auto_increment(&mut self) -> &mut Self
pub fn auto_increment(&mut self) -> &mut Self
Set column auto increment.
Maps to IDENTITY on MS SQL.
pub fn unique_key(&mut self) -> &mut Self
pub fn unique_key(&mut self) -> &mut Self
Set column unique constraint
pub fn primary_key(&mut self) -> &mut Self
pub fn primary_key(&mut self) -> &mut Self
Set column as primary key
pub fn char(&mut self) -> &mut Self
pub fn char(&mut self) -> &mut Self
Set column type as char
pub fn string_len(&mut self, length: u32) -> &mut Self
pub fn string_len(&mut self, length: u32) -> &mut Self
Set column type as string with custom length
pub fn string_max(&mut self) -> &mut Self
pub fn string_max(&mut self) -> &mut Self
Set column type as string with maximum length
pub fn string(&mut self) -> &mut Self
pub fn string(&mut self) -> &mut Self
Set column type as string
pub fn text(&mut self) -> &mut Self
pub fn text(&mut self) -> &mut Self
Set column type as text
pub fn tiny_integer(&mut self) -> &mut Self
pub fn tiny_integer(&mut self) -> &mut Self
Set column type as tiny_integer
pub fn small_integer(&mut self) -> &mut Self
pub fn small_integer(&mut self) -> &mut Self
Set column type as small_integer
pub fn integer(&mut self) -> &mut Self
pub fn integer(&mut self) -> &mut Self
Set column type as integer
pub fn big_integer(&mut self) -> &mut Self
pub fn big_integer(&mut self) -> &mut Self
Set column type as big_integer
pub fn tiny_unsigned(&mut self) -> &mut Self
pub fn tiny_unsigned(&mut self) -> &mut Self
Set column type as tiny_unsigned
pub fn small_unsigned(&mut self) -> &mut Self
pub fn small_unsigned(&mut self) -> &mut Self
Set column type as small_unsigned
pub fn unsigned(&mut self) -> &mut Self
pub fn unsigned(&mut self) -> &mut Self
Set column type as unsigned
pub fn big_unsigned(&mut self) -> &mut Self
pub fn big_unsigned(&mut self) -> &mut Self
Set column type as big_unsigned
pub fn float(&mut self) -> &mut Self
pub fn float(&mut self) -> &mut Self
Set column type as float
pub fn double(&mut self) -> &mut Self
pub fn double(&mut self) -> &mut Self
Set column type as double
pub fn decimal_len(&mut self, precision: u32, scale: u32) -> &mut Self
pub fn decimal_len(&mut self, precision: u32, scale: u32) -> &mut Self
Set column type as decimal with custom precision and scale
pub fn decimal(&mut self) -> &mut Self
pub fn decimal(&mut self) -> &mut Self
Set column type as decimal
pub fn date_time(&mut self) -> &mut Self
pub fn date_time(&mut self) -> &mut Self
Set column type as date_time
pub fn interval(
&mut self,
fields: Option<PgInterval>,
precision: Option<u32>,
) -> &mut Self
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("I1").interval(None, None).not_null())
.col(
ColumnDef::new("I2")
.interval(Some(PgInterval::YearToMonth), None)
.not_null()
)
.col(ColumnDef::new("I3").interval(None, Some(42)).not_null())
.col(
ColumnDef::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(" ")
);pub fn timestamp(&mut self) -> &mut Self
pub fn timestamp(&mut self) -> &mut Self
Set column type as timestamp
pub fn timestamp_with_time_zone(&mut self) -> &mut Self
pub fn timestamp_with_time_zone(&mut self) -> &mut Self
Set column type as timestamp with time zone. Postgres only
pub fn time(&mut self) -> &mut Self
pub fn time(&mut self) -> &mut Self
Set column type as time
pub fn date(&mut self) -> &mut Self
pub fn date(&mut self) -> &mut Self
Set column type as date
pub fn year(&mut self) -> &mut Self
pub fn year(&mut self) -> &mut Self
Set column type as year Only MySQL supports year
pub fn binary_len(&mut self, length: u32) -> &mut Self
pub fn binary_len(&mut self, length: u32) -> &mut Self
Set column type as binary with custom length
pub fn binary(&mut self) -> &mut Self
pub fn binary(&mut self) -> &mut Self
Set column type as binary with default length of 1
pub fn var_binary(&mut self, length: u32) -> &mut Self
pub fn var_binary(&mut self, length: u32) -> &mut Self
Set column type as binary with variable length
pub fn var_binary_max(&mut self) -> &mut Self
pub fn var_binary_max(&mut self) -> &mut Self
Set column type as binary with maximum length
pub fn blob(&mut self) -> &mut Self
pub fn blob(&mut self) -> &mut Self
Set column type as blob
pub fn boolean(&mut self) -> &mut Self
pub fn boolean(&mut self) -> &mut Self
Set column type as boolean
pub fn money_len(&mut self, precision: u32, scale: u32) -> &mut Self
pub fn money_len(&mut self, precision: u32, scale: u32) -> &mut Self
Set column type as money with custom precision and scale
pub fn money(&mut self) -> &mut Self
pub fn money(&mut self) -> &mut Self
Set column type as money
pub fn json(&mut self) -> &mut Self
pub fn json(&mut self) -> &mut Self
Set column type as json.
pub fn json_binary(&mut self) -> &mut Self
pub fn json_binary(&mut self) -> &mut Self
Set column type as json binary.
pub fn xml(&mut self) -> &mut Self
pub fn xml(&mut self) -> &mut Self
Set column type as XML. MS SQL only.
pub fn uuid(&mut self) -> &mut Self
pub fn uuid(&mut self) -> &mut Self
Set column type as uuid
pub fn custom<T>(&mut self, name: T) -> &mut Selfwhere
T: IntoIden,
pub fn custom<T>(&mut self, name: T) -> &mut Selfwhere
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(" ")
);pub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Self
pub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Self
Set column type as enum.
pub fn array(&mut self, elem_type: ColumnType) -> &mut Self
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.
pub fn cidr(&mut self) -> &mut Self
pub fn cidr(&mut self) -> &mut Self
Set columnt type as cidr. This is only supported on Postgres.
pub fn inet(&mut self) -> &mut Self
pub fn inet(&mut self) -> &mut Self
Set columnt type as inet. This is only supported on Postgres.
pub fn mac_address(&mut self) -> &mut Self
pub fn mac_address(&mut self) -> &mut Self
Set columnt type as macaddr. This is only supported on Postgres.
pub fn ltree(&mut self) -> &mut Self
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(" ")
);pub fn check<T>(&mut self, check: T) -> &mut Self
pub fn check<T>(&mut self, check: T) -> &mut Self
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) )"#,
);pub fn generated<T>(&mut self, expr: T, stored: bool) -> &mut Self
pub fn generated<T>(&mut self, expr: T, stored: bool) -> &mut Self
Sets the column as generated with Expr
use sea_query::{tests_cfg::*, *};
assert_eq!(
Table::create()
.table(Char::Table)
.if_not_exists()
.col(
ColumnDef::new(Char::Id)
.integer()
.not_null()
.primary_key()
.auto_increment(),
)
.col(
ColumnDef::new(Char::Ascii)
.boolean()
.not_null()
.generated(Expr::col(Char::FontSize).gt(0), false),
)
.to_string(PostgresQueryBuilder),
[
r#"CREATE TABLE IF NOT EXISTS "character" ("#,
r#""id" integer GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY,"#,
r#""ascii" bool NOT NULL GENERATED ALWAYS AS ("font_size" > 0) VIRTUAL"#,
r#")"#,
]
.join(" ")
);pub fn extra<T>(&mut self, string: T) -> &mut Self
pub fn extra<T>(&mut self, string: T) -> &mut Self
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 NOT NULL PRIMARY KEY DEFAULT gen_random_uuid(),"#,
r#""created_at" timestamp with time zone NOT NULL DEFAULT NOW()"#,
r#")"#,
]
.join(" ")
);pub fn using<T>(&mut self, value: T) -> &mut Self
pub fn using<T>(&mut self, value: T) -> &mut Self
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(" ")
);