Updating foreign keys on non-selected tables
wapodaca
Posts: 4
Here's the situation. I am having the same issue inSQL Compare 8.0, 8.5, and 9.
I'm trying to update a data structure that contains live data. I am attempting to add a single table which has no foreign keys. There is however another table that has a foreign key to the table I'm adding, but only in the new data structure. That second table is NOT selected. Update Dependencies is unchecked. When SQL Compare generates the sql script it puts in the Create for the first table but also adds the alter for the second table. This is unworkable because in a live date situation I need to be able to add the new table. Run an initialize script to load the values to it. Then update the second table so that it has the foreign key.
Am I missing a setting somewhere?
I'm trying to update a data structure that contains live data. I am attempting to add a single table which has no foreign keys. There is however another table that has a foreign key to the table I'm adding, but only in the new data structure. That second table is NOT selected. Update Dependencies is unchecked. When SQL Compare generates the sql script it puts in the Create for the first table but also adds the alter for the second table. This is unworkable because in a live date situation I need to be able to add the new table. Run an initialize script to load the values to it. Then update the second table so that it has the foreign key.
Am I missing a setting somewhere?
Comments
1. Run SQL Compare and add new tables only
2. Run SQL Data Compare to populate the new table
3. Run SQL Compare again to sync the rest of the schema
This seems a bit cumbersome, but I guess the only way it'd be more streamlined would be if SQL Compare and SQL Data Compare were combined into a single application so it could determine such dependencies.
Does Red Gate suggest any best practices for this scenario?
When I chose to sync just tables (excluding foreign keys), I got this error:
It's trying to remove an index on a table, but that index can't be removed because it's referenced by a foreign key. But since I've told it to ignore foreign keys, the script doesn't include the necessary DROP CONSTRAINT. (As a side note, the release notes for SQL Compare 9 say this has been fixed, but I'm using 9.0.0.79 and I still saw this error.) I tried adding IncludeDependencies to the options, as well as Default (based on feedback from another thread, heh) and IgnoreIndexes (hoping to push the whole mess off until the tables were synced) but none of those worked.
I managed to work around it by adding two more steps to my deployment script, here's what I eventually came up with:
1. Sync NEW tables only, excluding foreign key constraints. We need most of the default options except IncludeDependencies.
2. Sync data for all tables. This will include the tables we just added so we will be able to enforce the foreign key constraints on those new tables.
3. Sync ALL tables, this time we can include foreign keys because the new tables have already been added and populated. Again, we use the default options except IncludeDependencies.
4. Sync data for all tables again. This is unlikely to add anything new, unless we've added a new column to an existing static data table.
5. Sync the rest of the database objects.
This could still fail if you need to add a new primary key field to an existing static data table, populate that field, and then enforce a foreign key constraint against the new field. But that is a very unlikely scenario, so I'm considering this good enough for our purposes.
It still seems way more complicated than it needs to be, and I can't imagine I'm the first person to do this... do everyone's build scripts look like this, or am I just missing something?
(Edit: updated the options for steps 1 and 3; it was dropping and recreating all the indexes with the defaults excluded.)