From: Arthur Axel 'fREW' Schmidt Date: Thu, 24 Feb 2011 21:30:55 +0000 (-0600) Subject: Make true unique constraints if needed in SQL Server X-Git-Tag: v0.11008~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=66444b417642d9254c6d80f0636191cf7597f67d;p=dbsrgits%2FSQL-Translator.git Make true unique constraints if needed in SQL Server http://improvingsoftware.com/2010/03/26/creating-a-unique-constraint-that-ignores-nulls-in-sql-server/ --- diff --git a/Changes b/Changes index 12420b5..2b76c17 100644 --- a/Changes +++ b/Changes @@ -6,6 +6,7 @@ * Fix odd invocation of Test::More::pass() in t/36-filters.t (RT#64728) * Quote everything in SQL Server * Turn off constraints before dropping tables in SQL Server +* Make true unique constraints if needed in SQL Server # ---------------------------------------------------------- # 0.11007 2010-11-30 diff --git a/lib/SQL/Translator/Producer/SQLServer.pm b/lib/SQL/Translator/Producer/SQLServer.pm index c3ffee0..29058ba 100644 --- a/lib/SQL/Translator/Producer/SQLServer.pm +++ b/lib/SQL/Translator/Producer/SQLServer.pm @@ -302,9 +302,18 @@ sub produce { } elsif ( $type eq UNIQUE ) { $name = $name_ur || mk_name( $table_name . '_uc' ); - $c_def = - "CONSTRAINT $name UNIQUE " . - '(' . join( ', ', @fields ) . ')'; + my @nullable = grep { $_->is_nullable } $constraint->fields; + if (!@nullable) { + $c_def = + "CONSTRAINT $name UNIQUE " . + '(' . join( ', ', @fields ) . ')'; + } else { + push @index_defs, + "CREATE UNIQUE NONCLUSTERED INDEX $name_ur ON $table_name_ur (" . + join( ', ', @fields ) . ')' . + ' WHERE ' . join( ' AND ', map unreserve($_->name) . ' IS NOT NULL', @nullable ) . ';'; + next; + } } push @constraint_defs, $c_def; }