Hi,
How can I go about having a cross-platform compatible compound unique constraint over two columns where there can be only one record with NULL in one column and a given value in the other?
I want something like this:
UniqueConstraint("parent_id", "name")
except "parent_id" is nullable, and I want to disallow creating multiple records with parent_id=NULL and the same name. The problem here is that some database management systems, PostgreSQL for example, will treat all NULL values as not-equal inside a unique constraint, which makes sense for a single column unique, but not really for a multi-column one. This will allow multiple records with the same name and parent_id=NULL again.
In PostgreSQL, apparently I would need to create a unique index in addition to the constraint. I would really like to solve this without writing database-specific code, though.
Does anybody have a good solution for that?
Guido