Search code examples
androidandroid-roomcomposite-primary-key

Android room composite primary key, sort order


In order to migrate old db to room library, I have to make equivalent to line

primary key(id1, id2 desc)

from

create table t(
id1 integer not null, 
id2 integer not null,
.... 
primary key(id1, id2 desc)
)

I can't find how to define sorting order of composite indices or primary keys. It seems like Android room haven't particular annotations, or Annotation parameters if see androidx.room.Entity or androidx.room.Index.

Is it not supported? What are other ways to solve the problem? Create regular composite key on id1 and id2, and in all select queries set order by ?


Solution

  • Room supports raw queries via RoomDatabase.Callback so you can override onCreate method of callback and execute raw sql of creating index.