Issue when comparing tables with unique constraint

Hi,
I created an application using the Red-Gate SQL Comparison SDK 10 to compare two databases (which are identical by schema but differ in data) and synchronize them after the comparison.
Now I run into an issue for tables with a unique constraint. Let me explain it using an example.

The table definition looks something like this:
CREATE TABLE tbdRole
(
	fldRoleID INT IDENTITY (1, 1) NOT NULL PRIMARY KEY,
	fldRoleType INT NOT NULL,
	fldTemplateID INT NOT NULL,
	fldName VARCHAR(255) NOT NULL,
	fldReadRole BIT NOT NULL	
) 
 
-- make sure all roles for a template have a unique name
ALTER TABLE tbdRole 
ADD CONSTRAINT UN_tbdRole UNIQUE NONCLUSTERED 
(
	fldTemplateID, 
	fldName
)
Now, this table in the source database contains (amongst others) these two rows:
1001, 1, 12, "Role_1", 0
1002, 1, 13, "Role_1", 0

This table in the target databas contains (amongst others) these two rows:
1001, 1, 13, "Role_1", 0
1002, 1, 12, "Role_1", 0

When comparing these two tables, the following script is generated:
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
GO
SET DATEFORMAT YMD
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
-- Pointer used for text / image updates. This might not be needed, but is declared here just in case
DECLARE @pv binary(16)

-- Drop constraints from [dbo].[tbdRole]
ALTER TABLE [dbo].[tbdRole] DROP CONSTRAINT [FK_tbdRole_tbdTemplate]

-- Drop unused indexes from [dbo].[tbdRole]
DROP INDEX [IX_tbdRole_fldTemplateID] ON [dbo].[tbdRole]

-- Update 2 rows in [dbo].[tbdRole]
UPDATE [dbo].[tbdRole] SET [fldTemplateID]=12 WHERE [fldRoleID]=1001
UPDATE [dbo].[tbdRole] SET [fldTemplateID]=13 WHERE [fldRoleID]=1002

-- Add indexes to [dbo].[tbdRole]
CREATE CLUSTERED INDEX [IX_tbdRole_fldTemplateID] ON [dbo].[tbdRole] ([fldTemplateID]) ON [PRIMARY]

-- Add constraints to [dbo].[tbdRole]
ALTER TABLE [dbo].[tbdRole] WITH NOCHECK ADD CONSTRAINT [FK_tbdRole_tbdTemplate] FOREIGN KEY ([fldTemplateID]) REFERENCES [dbo].[tbdTemplate] ([fldTemplateID])
ALTER TABLE [dbo].[tbdRole] NOCHECK CONSTRAINT [FK_tbdRole_tbdTemplate]
COMMIT TRANSACTION
GO
When running this script, the following error occurs when executing the first UPDATE statement, because of the existing unique constraint:
Violation of UNIQUE KEY constraint 'UN_tbdRole'. Cannot insert duplicate key in object 'dbo.tbdRole'.
NOTE: I'm aware of the SqlOptions.DropConstraintsAndIndexes, but having set this or not does not matter. The exact same script is generated.

How can I address this issue?
Do I have to manually drop and recreate all unique contraints?

Comments

  • It doesn't look as if SQL Data Compare Engine is automatically setting UN_tbdRole as the comparison key, or mappings have not been created properly.

    AFAIK what you want to happen is a DELETE or INSERT rather than an UPDATE, if the row identifiers are the columns that are part of the UN_tbdRole constraint.
    tableMapping.MatchingMappings.Add(new
     FieldMapping(tableMapping.Obj1.Fields["fldTemplateID"], tableMapping.Obj2.Fields["fldTemplateID"], FieldMappingStatus.Success));
     tableMapping.MatchingMappings.Add(new
     FieldMapping(tableMapping.Obj1.Fields["fldName"], tableMapping.Obj2.Fields["fldName"], FieldMappingStatus.Success));
    
  • Yes Brian, that is correct:

    I rather would like a DELETE/INSERT instead of an UPDATE, because of the unique constraint, or a way to ignore the unique constraint.
    Do I achieve this with your posted code?

    Sidenote: the comparison key is set to the primary key of the table. We have some tables that have more than one unique key constraint, therefore we always set the comparison key on the primary key.
  • If you are not using the unique constraint columns as the comparison key, you should be able to get the script to run using the "DropConstraintsAndIndexes" option.
    EngineDataCompareOptions opts = new EngineDataCompareOptions(MappingOptions.Default, ComparisonOptions.Default, SqlOptions.Default | SqlOptions.DropConstraintsAndIndexes);
    // make sure to apply the "opts" value to ComparisonSession, Mappings, and SqlProvider for consistent results
    
  • No, I'm sorry.
    The script I posted was generated WITH the DropConstraintsAndIndexes turned ON:
    Dim objMappings As New SchemaMappings()
    objMappings.Options.ComparisonOptions = ComparisonOptions.Default
    objMappings.Options.MappingOptions = MappingOptions.Default
    objMappings.Options.SqlOptions = SqlOptions.Default
    objMappings.Options.SqlOptions = (objMappings.Options.SqlOptions Or SqlOptions.DisableKeys)
    objMappings.Options.SqlOptions = (objMappings.Options.SqlOptions Or SqlOptions.DropConstraintsAndIndexes)
    objMappings.Options.SqlOptions = (objMappings.Options.SqlOptions Or SqlOptions.DisableTriggers)
    ...
    objMappings.CreateMappings(objSourceDB, objTargetDB)
    ...           
    Using objComparisonSession As ComparisonSession = New ComparisonSession()
        objComparisonSession.Options = objMappings.Options
        objComparisonSession.CompareDatabases(objSourceDB, objTargetDB, objMappings)
        ...
        Using objExecutionBlock As ExecutionBlock = New SqlProvider() { _
                    .Options = objMappings.Options _
            }.GetMigrationSQL(objComparisonSession, True)
            New BlockExecutor().ExecuteBlock(objExecutionBlock, Configuration.SQLServer_Target, objDatabase.Target, Configuration.TrustedConnection_Target, Configuration.UserName_Target, Configuration.Password_Target)
        End Using
    End Using
    
    I'm using v10.0.1 of the SDK, if you might want to know this.

    Any other ideas? Am I missing something in the code?
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    Don't know. Will escalate this to the development team.
  • Thank you Brian.

    How will I receive updates on this issue? Do they also post in this topic?
  • Does UN_tbdRole exist in the table in both databases?
  • Yes, the database schema (tables, constraints, indexes, views, triggers, etc.) are exactly the same of both databases.
    Only the data differs.
  • If I use the following simplified code, the unique constraint is dropped and should prevent the error.
    Imports RedGate.SQLCompare.Engine
    Imports RedGate.SQLCompare.Engine.ReadFromFolder
    Imports RedGate.SQLDataCompare.Engine
    Imports RedGate.Shared.SQL.ExecutionBlock
    
    Module Module1
    
        Sub Main()
            Dim d As New Database
            Dim d2 As New Database
            Dim cp1 As New ConnectionProperties("localhost", "SDCTest1")
            Dim cp2 As New ConnectionProperties("localhost", "SDCTest2")
            d.RegisterForDataCompare(cp1)
            d2.RegisterForDataCompare(cp2)
            Dim opts As New EngineDataCompareOptions(MappingOptions.Default, ComparisonOptions.Default, SqlOptions.Default Or SqlOptions.DropConstraintsAndIndexes)
            Dim sess As New ComparisonSession
            sess.Options = opts
            Dim mappings As New TableMappings
            mappings.Options = opts
            mappings.CreateMappings(d.Tables, d2.Tables)
            For Each m As TableMapping In mappings
                m.Include = True
            Next
            Dim w As New Work
            sess.CompareDatabases(d, d2, mappings)
            Dim prov As New SqlProvider
            prov.Options = opts
            Dim eb As ExecutionBlock = prov.GetMigrationSQL(sess, True)
            Dim sqlcode As String = eb.GetString()
            Console.WriteLine(sqlcode)
    
        End Sub
    
    End Module
    
  • Hi, my code is nearly the same, except that in my case the "mappings" object is declared as "SchemaMappings" instead of "TableMappings".
    Could this be the reason? Could you test this?

    The reason why I use "SchemaMappings" is that the method "SQLDataCompare.Engine.DataCompareUserActions.ReplayUserActions" requires this object.
    I cannot convert a "TableMappings" to a "SchemaMappings" object to pass to this method.

    And I use the "ReplayUserActions" method, because some tables have to be excluded, and some tables have a WHERE clause to exclude some rows.
    (Sidenote for this case: the table "tbdRole" is not excluded and does not have a WHERE clause).
  • Far as I know, "SchemaMappings" refers explicitly to objects of type "Schema", ie if you wanted to map DBO to SCHEMA1 to some other schema -- it would not map any of the child objects of that schema, though.
  • Hm, your online help tells something different; the SchemaMappings is described as follows:
    "Holds mappings for schemas, users, or roles. Uses the database objects to create the mappings for the views and the tables from the two databases."
    http://help.red-gate.com/help/SQLDataCompareAPIv6/1/en/html/N_RedGate_SQLDataCompare_Engine.htm

    Anyway, all I want to achieve is that the SqlOption "DropConstraintsAndIndexes" actually does drop and recreate the unique constraint.
    The online help page about this describes the following:
    DropConstraintsAndIndexes - Drop and recreate primary keys, indexes, and unique constraints in the synchronization script. If the primary key, index, or unique constraint is the comparison key, it cannot be dropped.
    http://help.red-gate.com/help/SQLDataCompareAPIv6/1/en/html/T_RedGate_SQLDataCompare_Engine_SqlOptions.htm
    On the concerning table, the unique constraint is NOT used as the comparison key, so it should be dropped, but it isn't. Sounds like a bug to me.

    What do you suggest that I should modify in my code?
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    I'm sorry, I just do not know. I am working off the same documentation as you. I know that in my example, I do not use SchemaMappings and it's fine.

    I'm turning this support issue over to the development team.
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    I'd also like to point out, the doc link you have is for API v6.
  • What is de correct link for the v10 documentation, and is the information concerning this issue there different?
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    I would like to re-iterate: I cannot reproduce your issue. If you run my code, does it drop the constraint?

    I am using Redgate.SQLDataCompare.Engine v 10.0.1.101

    I cannot comment about the documnentation, I have once again notified the product management about the problems with the documentation.

    I would really like to get this working for you but I don't believe SchemaMappings are the way forward, but as you point out, I can't prove that with the documentation in the current state it is in.
  • Hi, I tried you suggestion, as suggested.

    I changed my code so that it is the same as yours:
    - I used the "TableMappings" instead of the "SchemaMappings"
    - I used the same "EngineDataCompareOptions" as you specfied, which includes the "DropConstraintsAndIndexes", and assigned it to the "TableMappings", "ComparisonSession" and "SqlProvider"
    - I do not call the method "ReplayUserActions" anymore
    - I added the "For...Next" loop to include all TableMappings after the "CreateMappings" call

    But:
    The unique constraint is NOT dropped!
    The script generated is somewhat bigger (because more tables are included), but regarding the concerning table ("tbdRole") the script is exactly the same:
    -- Drop constraints from [dbo].[tbdRole]
    ALTER TABLE [dbo].[tbdRole] DROP CONSTRAINT [FK_tbdRole_tbdTemplate]
    
    -- Drop unused indexes from [dbo].[tbdRole]
    DROP INDEX [IX_tbdRole_fldTemplateID] ON [dbo].[tbdRole]
    
    -- Update 2 rows in [dbo].[tbdRole]
    UPDATE [dbo].[tbdRole] SET [fldTemplateID]=12 WHERE [fldRoleID]=1001
    UPDATE [dbo].[tbdRole] SET [fldTemplateID]=13 WHERE [fldRoleID]=1002
    
    -- Add indexes to [dbo].[tbdRole]
    CREATE CLUSTERED INDEX [IX_tbdRole_fldTemplateID] ON [dbo].[tbdRole] ([fldTemplateID]) ON [PRIMARY]
    
    -- Add constraints to [dbo].[tbdRole]
    ALTER TABLE [dbo].[tbdRole] WITH NOCHECK ADD CONSTRAINT [FK_tbdRole_tbdTemplate] FOREIGN KEY ([fldTemplateID]) REFERENCES [dbo].[tbdtTemplate] ([fldTemplateID])
    ALTER TABLE [dbo].[tbdRole] NOCHECK CONSTRAINT [FK_tbdRole_tbdTemplate]
    
    Could it be that the difference in version of the component "Redgate.SQLDataCompare.Engine" is the reason?
    I currently use version 10.0.1.69.
    Is it possible that I can obtain your version (build 101), to test this?
  • I have found what did cause the problem!

    Apparently the unique contraint on the concerning table "tbdRole" was DISABLED.
    Therefore, your component does not generate the DROP statement for this constraint.

    This should be not a problem, because a disabled unique constraint would not fire when a duplicate value is inserted.

    But because a CLUSTERED index was also present on the table, this index is dropped.
    And dropping a clustered index results in a rebuild of ALL other indexes, including the unique constraint.
    This means that after the DROP INDEX statement, the unique constraint was enabled again, and prevents the data from being modified!

    The solution for me would be to enable all unique constraints first, because then your component does generate the DROP statements for them.

    But I would like to know why your component doesn't generate a DROP statement for disabled unique constraints, because dropping indexes could enabled them.
Sign In or Register to comment.