nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

Question: Unique() constraint that allow NULLS ?

Open cristian-eriomenco opened this issue 10 years ago • 2 comments

There is an interesting situation when is needed that a UNIQUE CONSTRAINT to take into account a WHERE clause. eg for SQL Server 2008:

CREATE UNIQUE NONCLUSTERED INDEX idx_yourcolumn_notnull
ON YourTable(yourcolumn)
WHERE yourcolumn IS NOT NULL;

There is a topic on StackOverflow regards this situation :
http://stackoverflow.com/questions/767657/how-do-i-create-a-unique-constraint-that-also-allows-nulls

So I'm wondering if there is some way of acheiving conditional unique constrains with fluent nhibernate.

cristian-eriomenco avatar Mar 11 '15 18:03 cristian-eriomenco

If I understand correctly, you create an index, and then Sql Server uses it internally to speed up a query or filter results. Seems to me that filter does not affect mapping from db tables to objects.

chester89 avatar Mar 11 '15 20:03 chester89

Hey Guys,

Is it possible you can configure the FluentNHibernate Map with .UniqueKey("idx_name") but also tell it not to generate any SQL?

I have the same situation as @cristian-eriomenco in that I want a unique that allows nulls. If I add the UniqueKey("idx_name") in the map, we forget and later generate a SQL column with a unique constraint incorrectly.

If you remove UniqueKey("idx_name") it prevents our FluentValidation working.

Is there a solution/workaround to this?

stokedout avatar Oct 24 '19 14:10 stokedout