Reputation: 2554
In my Android application I use Room library for persistency.
Assuming, I have an Entity defined like this:
@Entity(tableName = "my_entity")
public class MyEntity {
@ColumnInfo(name = "id")
@PrimaryKey(autoGenerate = true)
private int id;
//...
}
can I rely on the fact, that id
will be increased monotonically, i.e. that for newly inserted row id
will always be higher, than for all previously created rows?
I think, that it is unlikely, but I can imagine, that Room (or SQLite - I am not sure, who is responsible in this case) could e.g. try to reuse the IDs of the previously deleted rows...
As far as I can see, the official documentation does not tell anything about it PrimaryKey.AutoGenerate()
.
Upvotes: 1
Views: 228
Reputation: 2554
This answer is the expanded comment from JensV.
As suggested by JensV, the generated schema json file contains (among others):
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, ... <other fields>)"
So looking at the SQLite docs of AUTOINCREMENT we get, that it is guaranteed to be monotonic.
In fact, this flag serves exactly for this purpose: to ensure, that the generated value is monotonic (without this flag, the value still will be generated to be unique, but will not be necessarily monotonic). Taking into account, that Room uses the flag, it is strange, that they don't mention it in the documentation.
Upvotes: 1