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) -> SelfParameters
conflictResolutionAn optional conflict resolution (see https://www.sqlite.org/lang_conflict.html).
autoincrementIf 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) -> SelfParameters
conflictResolutionAn 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) -> SelfParameters
conflictResolutionAn 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() -> SelfReturn 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) -> SelfParameters
conditionA 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) -> SelfParameters
sqlAn 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) -> SelfParameters
valueA 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) -> SelfParameters
sqlAn SQL snippet.
Return Value
Self so that you can further refine the column definition.
-
Defines the default column collation.
try db.create(table: "player") { t in t.column("email", .text).collate(.nocase) }Declaration
Swift
@discardableResult public func collate(_ collation: Database.CollationName) -> SelfParameters
collationAn Database.CollationName.
Return Value
Self so that you can further refine the column definition.
-
Defines the default column collation.
try db.create(table: "player") { t in t.column("name", .text).collate(.localizedCaseInsensitiveCompare) }Declaration
Swift
@discardableResult public func collate(_ collation: DatabaseCollation) -> SelfParameters
collationA 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.
View on GitHub
Install in Dash
ColumnDefinition Class Reference