Foster auto-incremented primary keys #337
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces the
TableDefinition.autoIncrementedPrimaryKey
method, that defines an auto-incremented integer primary key. The documentation has been updated in order to foster this method:An auto-incremented primary key comes with the guarantee that ids are never reused. It is safer than an integer primary key that is not auto-incremented, and can reuse ids:
Imagine an application screen that displays and tracks the player with id 1. In the background, the application synchronizes the players with some server content. The player 1 is destroyed. A new and different player is inserted. If the primary key is not auto-incremented, SQLite can reuse the id 1 for the newly inserted player: the application screen may not notice that its player was destroyed, and may display the new player instead: this is a bug.
This kind of dangerous scenario is unfortunately likely to happen with the database observation tools that come with GRDB: FetchedRecordsController and RxGRDB. Even though both track changes in order to fetch fresh request results, they don't care about individual changes, and do not notice id reuses.
This is why we now foster the safest option: auto-incremented primary keys. Expert users can still use naked integer primary keys when they need.