pub struct ColumnDef { /* private fields */ }
Expand description
Specification of a table column
Implementations§
source§impl ColumnDef
impl ColumnDef
sourcepub 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
sourcepub fn default<T>(&mut self, value: T) -> &mut Selfwhere
T: Into<SimpleExpr>,
pub fn default<T>(&mut self, value: T) -> &mut Selfwhere 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(" ")
);
sourcepub fn auto_increment(&mut self) -> &mut Self
pub fn auto_increment(&mut self) -> &mut Self
Set column auto increment
sourcepub fn unique_key(&mut self) -> &mut Self
pub fn unique_key(&mut self) -> &mut Self
Set column unique constraint
sourcepub fn primary_key(&mut self) -> &mut Self
pub fn primary_key(&mut self) -> &mut Self
Set column as primary key
sourcepub fn char_len(&mut self, length: u32) -> &mut Self
pub fn char_len(&mut self, length: u32) -> &mut Self
Set column type as char with custom length
sourcepub 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
sourcepub fn tiny_integer(&mut self) -> &mut Self
pub fn tiny_integer(&mut self) -> &mut Self
Set column type as tiny_integer
sourcepub fn small_integer(&mut self) -> &mut Self
pub fn small_integer(&mut self) -> &mut Self
Set column type as small_integer
sourcepub fn big_integer(&mut self) -> &mut Self
pub fn big_integer(&mut self) -> &mut Self
Set column type as big_integer
sourcepub fn tiny_unsigned(&mut self) -> &mut Self
pub fn tiny_unsigned(&mut self) -> &mut Self
Set column type as tiny_unsigned
sourcepub fn small_unsigned(&mut self) -> &mut Self
pub fn small_unsigned(&mut self) -> &mut Self
Set column type as small_unsigned
sourcepub fn big_unsigned(&mut self) -> &mut Self
pub fn big_unsigned(&mut self) -> &mut Self
Set column type as big_unsigned
sourcepub 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
sourcepub 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(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(" ")
);
sourcepub 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
sourcepub fn year(&mut self, length: Option<MySqlYear>) -> &mut Self
pub fn year(&mut self, length: Option<MySqlYear>) -> &mut Self
Set column type as year Only MySQL supports year
sourcepub 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
sourcepub fn blob(&mut self, size: BlobSize) -> &mut Self
pub fn blob(&mut self, size: BlobSize) -> &mut Self
Set column type as blob, but when given BlobSize::Blob(size) argument, this column map to binary(size) type instead.
sourcepub 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
sourcepub fn bit(&mut self, length: Option<u32>) -> &mut Self
pub fn bit(&mut self, length: Option<u32>) -> &mut Self
Set column type as bit with variable length
sourcepub fn varbit(&mut self, length: u32) -> &mut Self
pub fn varbit(&mut self, length: u32) -> &mut Self
Set column type as varbit with variable length
sourcepub 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
sourcepub fn json_binary(&mut self) -> &mut Self
pub fn json_binary(&mut self) -> &mut Self
Set column type as json binary.
sourcepub 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.
sourcepub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Selfwhere
N: IntoIden,
S: IntoIden,
V: IntoIterator<Item = S>,
pub fn enumeration<N, S, V>(&mut self, name: N, variants: V) -> &mut Selfwhere N: IntoIden, S: IntoIden, V: IntoIterator<Item = S>,
Set column type as enum.
sourcepub 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.
sourcepub fn cidr(&mut self) -> &mut Self
pub fn cidr(&mut self) -> &mut Self
Set columnt type as cidr. This is only supported on Postgres.
sourcepub fn inet(&mut self) -> &mut Self
pub fn inet(&mut self) -> &mut Self
Set columnt type as inet. This is only supported on Postgres.
sourcepub 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.
sourcepub fn check<T>(&mut self, value: T) -> &mut Selfwhere
T: Into<SimpleExpr>,
pub fn check<T>(&mut self, value: T) -> &mut Selfwhere T: Into<SimpleExpr>,
Set constraints as SimpleExpr
sourcepub fn generated<T>(&mut self, expr: T, stored: bool) -> &mut Selfwhere
T: Into<SimpleExpr>,
pub fn generated<T>(&mut self, expr: T, stored: bool) -> &mut Selfwhere T: Into<SimpleExpr>,
Sets the column as generated with SimpleExpr
sourcepub fn extra<T>(&mut self, string: T) -> &mut Selfwhere
T: Into<String>,
pub fn extra<T>(&mut self, string: T) -> &mut Selfwhere 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 uuid_generate_v4()")
.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 uuid_generate_v4() PRIMARY KEY NOT NULL,"#,
r#""created_at" timestamp with time zone DEFAULT NOW() NOT NULL"#,
r#")"#,
]
.join(" ")
);