6

I'm using a Room Sqlite database on Android. I have the following database entities:

@Entity
public static class TopEntry {
    @Ignore
    public DatabaseDAOs.BaseDAO dao;
}

@Entity
public static class DatabaseEntry extends TopEntry {
    @PrimaryKey(autoGenerate = true)
    public long id;

    @PrimaryKey
    public long sessionID;

    public Long eventNanoTime;
    public Long milliTimestamp;
}

As you can see, I would like to have a composite primary key. One primary key should be auto-incremented.

This is not possible and throws an error:

You cannot have multiple primary keys defined in an Entity. If you want to declare a composite primary key, you should use @Entity#primaryKeys and not use @PrimaryKey

How can I implement this in Room?

machinery
  • 5,972
  • 12
  • 67
  • 118
  • 2
    [This answer](https://stackoverflow.com/a/6793274/4815718) to a similar question indicates SQLite does not support autoincrement with composite primary key. – Bob Snyder Jul 16 '20 at 15:49
  • @BobSnyder Thank you bob. In the link it is mentioned that a possibility is to use one id as primary key with auto increment and sessionId with unique constrained. But will that work? If I would have two entries with same id but different sessionID, would this throw a primary key exception? – machinery Jul 16 '20 at 16:36

0 Answers0