PrimaryKeyInfo

public struct PrimaryKeyInfo

You get primary keys from table names, with the Database.primaryKey(_) method.

Primary key is nil when table has no primary key:

// CREATE TABLE items (name TEXT)
let itemPk = try db.primaryKey("items") // nil

Primary keys have one or several columns. When the primary key has a single column, it may contain the row id:

// CREATE TABLE persons (
//   id INTEGER PRIMARY KEY,
//   name TEXT
// )
let personPk = try db.primaryKey("persons")!
personPk.columns     // ["id"]
personPk.rowIDColumn // "id"

// CREATE TABLE countries (
//   isoCode TEXT NOT NULL PRIMARY KEY
//   name TEXT
// )
let countryPk = db.primaryKey("countries")!
countryPk.columns     // ["isoCode"]
countryPk.rowIDColumn // nil

// CREATE TABLE citizenships (
//   personID INTEGER NOT NULL REFERENCES persons(id)
//   countryIsoCode TEXT NOT NULL REFERENCES countries(isoCode)
//   PRIMARY KEY (personID, countryIsoCode)
// )
let citizenshipsPk = db.primaryKey("citizenships")!
citizenshipsPk.columns     // ["personID", "countryIsoCode"]
citizenshipsPk.rowIDColumn // nil
  • The columns in the primary key; this array is never empty.

    Declaration

    Swift

    public var columns: [String]
  • When not nil, the name of the column that contains the INTEGER PRIMARY KEY.

    Declaration

    Swift

    public var rowIDColumn: String?