Can I use a nullable column in the primary key? #2899
-
We have a composite key on a table:
In this case, Ideas on a work-around? There is an additional column we have that might serve as a better uniqueness indicator, but I wanted to know if there was a better built-in way to having Drift treat two |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
FWIW, I've found some discussion that nullable columns in a primary key is not always standard database design, and it seems like some databases will not treat two |
Beta Was this translation helpful? Give feedback.
-
There's not much drift could do here since it just uses sqlite3 under the hood, which documents this as follows:
So it seems like yes, this is the expected (although not ideal) behavior and |
Beta Was this translation helpful? Give feedback.
-
Gotcha, so, column3 really shouldn't be non-nullable - we were going back and forth between having a default Let's say I changed the primary key to be a different column that might be a better indicated of uniqueness
I tried this, but I got conflicts because there were duplicate rows in the database that conflicted after this constraint. (If the column3 How would I resolve primaryKey constraint failures during a migration?? In this case I wouldn't care which of the duplicates were kept. And sorry if this is better documented elsewhere, I've given the migration docs a look and didn't see anything about this. Thanks |
Beta Was this translation helpful? Give feedback.
It's a bit counter-intuitive, but you could apply a type converter mapping from nullable values in Dart to a non-null value in SQL. But then it's important to remember that SQL features like
IS NULL
will no longer work.The best way to drop duplicates (that I can think of) is to use a custom query with common table expressions. I think this query would drop duplicates across
column1
,column2
andcolumn4
, for instance: