Skip to content

How to add a unique index on an existing table column? #1782

Answered by groue
pedrommcarrasco asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @pedrommcarrasco,

Is there a clean way to migrate this table now to enforce uniqueness on the key column and have it properly reflected in the schema?

Please follow precisely the instructions at How to Recreate a Database Table.

You have to be careful because the badges table may contain duplicate entries for a given key, due to the lack of unique index. You MUST come up with a strategy for disambiguating those entries, here, so that you have the guarantee that the fix runs correctly on all user devices.

In the following sample code, I'll prefer the most "recent" row, by picking the row with the maximum rowid (a value that all SQLite tables have, and is usually growing). You will …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@pedrommcarrasco
Comment options

@groue
Comment options

@pedrommcarrasco
Comment options

Answer selected by pedrommcarrasco
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants