Search code examples
androidandroid-room

How does auto-generation of identifiers work in Room Database?


There's a situation when in the database there are three records with identifiers 1, 2, 3 respectively. If I delete the first record, only records 2 and 3 will remain. When I add a new record, will it have an identifier 1 or 4?


Solution

  • 1- You have records with identifiers 1, 2, and 3.

    2- You delete the record with identifier 1.

    3- When you insert a new record, the Room Database will automatically assign the next available identifier, which is 4 in this case. This is because it keeps track of the highest identifier used, and it will generate the next one accordingly.

    So, if you insert a new record after deleting the first one, it will have an identifier of 4. Room Database ensures that identifiers are unique and auto-increments based on the highest identifier in the table.