drift icon indicating copy to clipboard operation
drift copied to clipboard

Provide a way to access auto-generated rowid on dataclasses

Open micimize opened this issue 4 years ago • 3 comments

From the sqlite docs:

The rowid (and "oid" and "rowid") is omitted in WITHOUT ROWID tables. WITHOUT ROWID tables are only available in SQLite version 3.8.2 (2013-12-06) and later. A table that lacks the WITHOUT ROWID clause is called a "rowid table".

This poses an issue if one wants to access rowid on a table with a custom primaryKey, but not as a user-defined column

micimize avatar Apr 20 '21 15:04 micimize

FWIW this has gotten a bit better now:

  • In selects, you can access the rowid column in Dart and add it via a join or selectOnly.
  • Recently (see the linked PR), the rowid would also be exposed on companions so it can be set to custom values for inserts or updates.

simolus3 avatar Feb 20 '23 21:02 simolus3

@simolus3 Is there a concise way to use selectOnly to get a select rowId, * from table?

That said, I don't entirely remember what I wanted rowId for in and haven't touched the Flutter app I was working on in a while, so I can't say if that solves my issue. Though, I'm guessing I explicitly wanted to access rowId from companions.

micimize avatar Feb 28 '23 15:02 micimize

Is there a concise way to use selectOnly to get a select rowId, * from table?

select(table).addColumns([table.rowId]) should work. You'll get a TypedResult back on which you can use readTable(table) to get the row class for the table and read(table.rowid) to get its rowid.

simolus3 avatar Mar 01 '23 08:03 simolus3