ColumnDefinition
public final class ColumnDefinition
The ColumnDefinition class lets you refine a table column.
You get instances of this class when you create or alter a database table:
try db.create(table: "player") { t in
t.column(...) // ColumnDefinition
}
try db.alter(table: "player") { t in
t.add(column: ...) // ColumnDefinition
}
See https://www.sqlite.org/lang_createtable.html and https://www.sqlite.org/lang_altertable.html
-
Adds a primary key constraint on the column.
try db.create(table: "player") { t in t.column("id", .integer).primaryKey() }
See https://www.sqlite.org/lang_createtable.html#primkeyconst and https://www.sqlite.org/lang_createtable.html#rowid
Declaration
Swift
@discardableResult public func primaryKey( onConflict conflictResolution: Database.ConflictResolution? = nil, autoincrement: Bool = false) -> Self
Parameters
conflictResolution
An optional conflict resolution (see https://www.sqlite.org/lang_conflict.html).
autoincrement
If true, the primary key is autoincremented.
Return Value
Self so that you can further refine the column definition.
-
Adds a NOT NULL constraint on the column.
try db.create(table: "player") { t in t.column("name", .text).notNull() }
See https://www.sqlite.org/lang_createtable.html#notnullconst
Declaration
Swift
@discardableResult public func notNull(onConflict conflictResolution: Database.ConflictResolution? = nil) -> Self
Parameters
conflictResolution
An optional conflict resolution (see https://www.sqlite.org/lang_conflict.html).
Return Value
Self so that you can further refine the column definition.
-
Adds a UNIQUE constraint on the column.
try db.create(table: "player") { t in t.column("email", .text).unique() }
See https://www.sqlite.org/lang_createtable.html#uniqueconst
Declaration
Swift
@discardableResult public func unique(onConflict conflictResolution: Database.ConflictResolution? = nil) -> Self
Parameters
conflictResolution
An optional conflict resolution (see https://www.sqlite.org/lang_conflict.html).
Return Value
Self so that you can further refine the column definition.
-
Adds an index of the column.
try db.create(table: "player") { t in t.column("email", .text).indexed() }
See https://www.sqlite.org/lang_createtable.html#uniqueconst
Declaration
Swift
@discardableResult public func indexed() -> Self
Return Value
Self so that you can further refine the column definition.
-
Adds a CHECK constraint on the column.
try db.create(table: "player") { t in t.column("name", .text).check { length($0) > 0 } }
Declaration
Swift
@discardableResult public func check(_ condition: (Column) -> SQLExpressible) -> Self
Parameters
condition
A closure whose argument is an Column that represents the defined column, and returns the expression to check.
Return Value
Self so that you can further refine the column definition.
-
Adds a CHECK constraint on the column.
try db.create(table: "player") { t in t.column("name", .text).check(sql: "LENGTH(name) > 0") }
Declaration
Swift
@discardableResult public func check(sql: String) -> Self
Parameters
sql
An SQL snippet.
Return Value
Self so that you can further refine the column definition.
-
Defines the default column value.
try db.create(table: "player") { t in t.column("name", .text).defaults(to: "Anonymous") }
Declaration
Swift
@discardableResult public func defaults(to value: DatabaseValueConvertible) -> Self
Parameters
value
A DatabaseValueConvertible value.
Return Value
Self so that you can further refine the column definition.
-
Defines the default column value.
try db.create(table: "player") { t in t.column("creationDate", .DateTime).defaults(sql: "CURRENT_TIMESTAMP") }
Declaration
Swift
@discardableResult public func defaults(sql: String) -> Self
Parameters
sql
An SQL snippet.
Return Value
Self so that you can further refine the column definition.
-
try db.create(table: "player") { t in t.column("email", .text).collate(.nocase) }
Declaration
Swift
@discardableResult public func collate(_ collation: Database.CollationName) -> Self
Parameters
collation
An Database.CollationName.
Return Value
Self so that you can further refine the column definition.
-
try db.create(table: "player") { t in t.column("name", .text).collate(.localizedCaseInsensitiveCompare) }
Declaration
Swift
@discardableResult public func collate(_ collation: DatabaseCollation) -> Self
Parameters
collation
A custom DatabaseCollation.
Return Value
Self so that you can further refine the column definition.
-
Defines a foreign key.
try db.create(table: "book") { t in t.column("authorId", .integer).references("author", onDelete: .cascade) }
Declaration
Return Value
Self so that you can further refine the column definition.