TableDefinition
public final class TableDefinition
The TableDefinition class lets you define table columns and constraints.
You don’t create instances of this class. Instead, you use the Database
create(table:) method:
try db.create(table: "players") { t in // t is TableDefinition
t.column(...)
}
-
Appends a table column.
try db.create(table: "players") { t in t.column("name", .text) }See https://www.sqlite.org/lang_createtable.html#tablecoldef
Declaration
Swift
public func column(_ name: String, _ type: Database.ColumnType? = nil) -> ColumnDefinitionParameters
namethe column name.
typethe eventual column type.
Return Value
An ColumnDefinition that allows you to refine the column definition.
-
Defines the table primary key.
try db.create(table: "citizenships") { t in t.column("citizenID", .integer) t.column("countryCode", .text) t.primaryKey(["citizenID", "countryCode"]) }See https://www.sqlite.org/lang_createtable.html#primkeyconst and https://www.sqlite.org/lang_createtable.html#rowid
Declaration
Swift
public func primaryKey(_ columns: [String], onConflict conflictResolution: Database.ConflictResolution? = nil)Parameters
columnsThe primary key columns.
conflitResolutionAn optional conflict resolution (see https://www.sqlite.org/lang_conflict.html).
-
Adds a unique key.
try db.create(table: "pointOfInterests") { t in t.column("latitude", .double) t.column("longitude", .double) t.uniqueKey(["latitude", "longitude"]) }See https://www.sqlite.org/lang_createtable.html#uniqueconst
Declaration
Swift
public func uniqueKey(_ columns: [String], onConflict conflictResolution: Database.ConflictResolution? = nil)Parameters
columnsThe unique key columns.
conflitResolutionAn optional conflict resolution (see https://www.sqlite.org/lang_conflict.html).
-
Adds a foreign key.
try db.create(table: "passport") { t in t.column("issueDate", .date) t.column("citizenID", .integer) t.column("countryCode", .text) t.foreignKey(["citizenID", "countryCode"], references: "citizenships", onDelete: .cascade) }Declaration
Parameters
columnsThe foreign key columns.
tableThe referenced table.
destinationColumnsThe columns in the referenced table. If not specified, the columns of the primary key of the referenced table are used.
deleteActionOptional action when the referenced row is deleted.
updateActionOptional action when the referenced row is updated.
deferredIf true, defines a deferred foreign key constraint. See https://www.sqlite.org/foreignkeys.html#fk_deferred.
-
Adds a CHECK constraint.
try db.create(table: "players") { t in t.column("personalPhone", .text) t.column("workPhone", .text) let personalPhone = Column("personalPhone") let workPhone = Column("workPhone") t.check(personalPhone != nil || workPhone != nil) }Declaration
Swift
public func check(_ condition: SQLExpressible)Parameters
conditionThe checked condition
-
Adds a CHECK constraint.
try db.create(table: "players") { t in t.column("personalPhone", .text) t.column("workPhone", .text) t.check(sql: "personalPhone IS NOT NULL OR workPhone IS NOT NULL") }Declaration
Swift
public func check(sql: String)Parameters
sqlAn SQL snippet
View on GitHub
Install in Dash
TableDefinition Class Reference