SQLite.swift icon indicating copy to clipboard operation
SQLite.swift copied to clipboard

Primary Key restricted to Int64?

Open russbishop opened this issue 11 years ago • 5 comments

Is there any reason to force the primary key to be Int64? Sqlite supports BLOB primary keys which work rather well for UUIDs.

russbishop avatar Mar 04 '15 08:03 russbishop

Did you read the documentation on the primaryKey function?

Only the shorthand should be restricted, since the basic use-case is an integer that autoincrements:

db.create(table: users) { t in
    t.column(id, primaryKey: true)
}
// CREATE TABLE "users" ("id" INTEGER PRIMARY KEY NOT NULL)

You should be able to create a primary key of any type if you use the primaryKey function:

let uuid = Expression<Blob>("uuid")
db.create(table: users) { t in
    t.column(uuid)
    t.primaryKey(uuid)
}
// CREATE TABLE "users" ("uuid" BLOB NOT NULL, PRIMARY KEY("uuid"))

stephencelis avatar Mar 04 '15 08:03 stephencelis

Closing for this now. Please let me know if it actually doesn't work for you!

stephencelis avatar Mar 04 '15 16:03 stephencelis

Ok, I obviously didn't pay enough attention. It might be helpful to call this out in the docs.

Thanks!

russbishop avatar Mar 04 '15 17:03 russbishop

It might be helpful to call this out in the docs.

Hm...I try to! Here:

primaryKey adds an INTEGER PRIMARY KEY constraint to a single column. (See the primaryKey function under Table Constraints for non-integer primary keys).

— Documented in "Column Constraints"

Any ideas on how to better communicate this?

stephencelis avatar Mar 04 '15 17:03 stephencelis

Honestly, rather than do that I just went ahead and submitted a PR #76 to remove the restriction

russbishop avatar Mar 04 '15 18:03 russbishop