I've got two fields in my table that if they(tat and dim) are equal I'm discarding them i.e
id| tat | dim | visible
1 | 11 | 22 | true
2 | 11 | 22 | false
This is considered not to be valid entry for ids 1
and 2
. Because I want to allow one dim
per tat
. So I have a script that cleaned these duplicates for me and after that I added index on this table to prevent this from happening like this :
CREATE UNIQUE INDEX dim_tat_idx
ON predictions (tat, dim);
As explained quite nicely in this answer:
https://mcmap.net/q/749819/-how-to-add-a-conditional-unique-index-on-postgresql
Now going further in this case
This way you can enter (1,2), (1,3) and (1, NULL)
but neither of them a second time
What type of index would I create or what other mechanism would I use at the database level to allow entering 1, NULL
combination but prevent (1,2)
and (1,3)
more times?
(1,NULL)
with the unique index you have. – Krusche