When backing up tables with indices containing a WHERE
clause, the condition does not get saved.
This is an issue for me since I have a DB with a "unique if not NULL" constraint that doesn't get backed up properly.
Example (SQLServer).
Create the following table in a database:
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
DROP TABLE IF EXISTS test
GO
CREATE TABLE test(
id int PRIMARY KEY,
nullable_key varchar(255) NULL
)
GO
CREATE UNIQUE INDEX uqn_index_nullable_key
ON test(nullable_key)
WHERE nullable_key IS NOT NULL
GO
INSERT INTO test VALUES (0, 'a'), (1, NULL), (2, NULL)
GO
Creating a backup on this database using SQribe results in the following TableIndexes.sql file:
SET NUMERIC_ROUNDABORT OFF
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
SET XACT_ABORT ON
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO -- SQRIBE/GO;4bae80
PRINT N'CREATE indices'
GO -- SQRIBE/GO;4bae80
-- SQRIBE/OBJ;4bae80
CREATE UNIQUE NONCLUSTERED INDEX [uqn_index_nullable_key] ON [dbo].[test] ([nullable_key] ASC)
WITH (PAD_INDEX=OFF, ALLOW_PAGE_LOCKS=ON, ALLOW_ROW_LOCKS=ON, STATISTICS_NORECOMPUTE=OFF, IGNORE_DUP_KEY=OFF, SORT_IN_TEMPDB=OFF, FILLFACTOR=100) ON [PRIMARY];
GO -- SQRIBE/GO;4bae80
We can see that the generated file does not keep the WHERE nullable_key IS NOT NULL
part, which result in a failing restoration due to duplicate NULL
values violating the index.
- EXCEPTION: Helpers.OpenExec(); One or more errors occurred. (The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.test' and the index name 'uqn_index_nullable_key'. The duplicate key value is (<NULL>).)
Any way this could get resolved, and is there a workaround that I can use in the meantime?
Thanks in advance!