Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
helje5 opened this issue Aug 23, 2022 · 1 comment
Closed

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

helje5 opened this issue Aug 23, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@helje5
Copy link
Member

helje5 commented Aug 23, 2022

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 helje5 added the enhancement New feature or request label Aug 23, 2022
@helje5 helje5 self-assigned this Aug 24, 2022
@helje5
Copy link
Member Author

helje5 commented Aug 24, 2022

Implemented, using Int.min

@helje5 helje5 closed this as completed Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant