New bug in comparing Foreign Key Relationships
JohnnyT
Posts: 20
Thank you for adding the "Use DROP and CREATE instead of ALTER" option that we requested. This will be a big help when deploying update scripts to clients who may already have pre-release versions of stored procedures installed on their live databases.
However, this option introduced a serious new bug when comparing foreign key constraints. If a FK constraint is not enforced (Enforce Foreign Key Constraint = No), the generated deployment script neglects to disable enforcement of that constraint after it is rebuilt. So those constraints now have "Enforce Foreign Key Constraint" = Yes. I can reproduce this bug.
This bug is not present when I uncheck the new "Add object existence checks" and "Use DROP and CREATE instead of ALTER" options. It's also not present in SQL Compare 9. These properly generate the "Disabling constraints on [tablename]" sections in the deployment script.
One question... Why does this new "Use DROP and CREATE instead of ALTER" cause many things such as FK constraints to be dropped and created? I thought this option would only affect server side code such as stored procedures and functions. Hopefully it's not dropping and recreating tables without migrating data to the rebuilt tables.
However, this option introduced a serious new bug when comparing foreign key constraints. If a FK constraint is not enforced (Enforce Foreign Key Constraint = No), the generated deployment script neglects to disable enforcement of that constraint after it is rebuilt. So those constraints now have "Enforce Foreign Key Constraint" = Yes. I can reproduce this bug.
This bug is not present when I uncheck the new "Add object existence checks" and "Use DROP and CREATE instead of ALTER" options. It's also not present in SQL Compare 9. These properly generate the "Disabling constraints on [tablename]" sections in the deployment script.
One question... Why does this new "Use DROP and CREATE instead of ALTER" cause many things such as FK constraints to be dropped and created? I thought this option would only affect server side code such as stored procedures and functions. Hopefully it's not dropping and recreating tables without migrating data to the rebuilt tables.
Comments
Then it rebuilds it...
Then it attempts to mark it as unenforced...
However, the logic added by the "Add object existence checks" option is not correct. It should be "IF EXISTS", not "IF NOT EXISTS". The way it is now, it remains enforced. Thanks.
I'm not sure this is a consequence of the existence checks option - it looks to me this is just part of the script that runs when a foreign key needs to be dropped and re-added. Foreign Keys are not considered as "free-standing" objects by SQL Compare and shouldn't be affected by the check existence option.
The last time I checked, SQL Compare always created foreign keys with NOCHECK so as to prevent failure in the schema script when existing data was inconsistent. This should only affect the creation of the key, and not disable the key permanently, though.
Hopefully this can be fixed with a patch release soon, as using these two new options can really screw up deployments. I'm just glad I caught this before I deployed it to my 200+ clients.
Thanks,
John
For instance, the IF NOT EXISTS is not incorrect - it's checking to see the FK doesn't exist before it puts the FK back.
I think that when you use the existence check it probably causes the table to be rebuilt and that would require the FK to be taken off and put back on.
And then when it does that it applies the key with nocheck so the script will succeed even if the data doesn't conform.
ftp://support.red-gate.com/patches/SQL_ ... .0.181.zip
Let us know how you get on with it.