Lighter-swift / Lighter

Swift APIs for SQLite: Type-safe down to the schema. Very, very, fast. Dependency free.
https://lighter-swift.github.io/documentation/lighter/
MIT License
459 stars 12 forks source link

TBD: Add a default for SQLite generated primary keys #10

Closed helje5 closed 2 years ago

helje5 commented 2 years ago

Inserts for integer primary keys use SQLite to generate the actual key, e.g.

CREATE TABLE person (
  person_id INTEGER PRIMARY KEY NOT NULL,
  name TEXT
);

This is generated into:

struct Person {
  var id : Int
  var name : String?
  init(id: Int, name: String? = nil) {...}
}

Which is a little inconvenient for inserts:

var person = Person(id: 0, name: "Donald")
person = try db.insert(person)

(the return value of the insert will have copy of the record with the proper db assigned primary key).

Maybe this should be some, ideally unlikely, obscure default value, like:

init(id: Int = MyDB.defaultIntPrimaryKey, name: String? = nil) { ... }
extension MyDB {
  static let MyDB.defaultIntPrimaryKey : Int = -0xDEADBEEF
}

Not quite sure whether that is actually good or not, maybe it is OK as an optional generation option.

helje5 commented 2 years ago

Implemented, using Int.min