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
-
The
GeneratedColumnQualification
enum defines whether a generated column sis virtual or stored.See https://sqlite.org/gencol.html#virtual_versus_stored_columns
See more -
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
-
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
-
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
-
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
-
Adds a CHECK constraint on the column.
try db.create(table: "player") { t in t.column("name", .text).check { length($0) > 0 } }
-
Adds a CHECK constraint on the column.
try db.create(table: "player") { t in t.column("name", .text).check(sql: "LENGTH(name) > 0") }
-
Defines the default column value.
try db.create(table: "player") { t in t.column("name", .text).defaults(to: "Anonymous") }
-
Defines the default column value.
try db.create(table: "player") { t in t.column("creationDate", .DateTime).defaults(sql: "CURRENT_TIMESTAMP") }
-
Defines the default column collation.
try db.create(table: "player") { t in t.column("email", .text).collate(.nocase) }
-
Defines the column as a generated column.
try db.create(table: "player") { t in t.column("id", .integer).primaryKey() t.column("score", .integer).notNull() t.column("bonus", .integer).notNull() t.column("totalScore", .integer).generatedAs(sql: "score + bonus", .stored) }
See https://sqlite.org/gencol.html. Note particularly the limitations of generated columns, e.g. they may not have a default value.
-
Defines the column as a generated column.
try db.create(table: "player") { t in t.column("id", .integer).primaryKey() t.column("score", .integer).notNull() t.column("bonus", .integer).notNull() t.column("totalScore", .integer).generatedAs(Column("score") + Column("bonus"), .stored) }
See https://sqlite.org/gencol.html. Note particularly the limitations of generated columns, e.g. they may not have a default value.
-
Defines a foreign key.
try db.create(table: "book") { t in t.column("authorId", .integer).references("author", onDelete: .cascade) }