I'm trying to migrate to room, but the schema of my table was like this:
CREATE TABLE cache(key text, content text, time integer);
Entity:
@Entity(tableName = "cache")
public class Cache{
public Integer id;
public String key;
public String content;
public Integer time;
}
No primary key was declared explicit, an error would occur while build:
An entity must have at least 1 field annotated with @PrimaryKey
I tried to add primary key to the table, but seems sqlite does not support that, anyone could help me?
In short you cannot utilise primary keys that do not have the NOT NULL constraint, when Room builds the schema it is always applied to primary keys.
Surrogate keys In such cases, a surrogate key can be used instead as the primary key.
Every table can have (but does not have to have) a primary key. The column or columns defined as the primary key ensure uniqueness in the table; no two rows can have the same key.
Excerpt from here: http://www.sqlitetutorial.net/sqlite-primary-key/
Unlike other database systems e.g., MySQL, PostgreSQL, etc., you cannot use the ALTER TABLE statement to add a primary key to an existing table.
To work around this, you need to:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With