Insert Script fails because Foreign Keys in Shadow Database
I have a very simple script that I want to include within my database project. It inserts a record into a table.
I can deploy fine but when I try to "refresh" from the Ready Roll pane I get a SQL Server error that the script fails because of a foreign key violation. This is obviously because the Shadow database contains no data but still contains foreign keys.
How do I get around this? I have read the articles on data population scripts but they all seem to assume you are deploying to a blank database. Where are you supposed to put maintenance/bug-fix scripts like this?
I can deploy fine but when I try to "refresh" from the Ready Roll pane I get a SQL Server error that the script fails because of a foreign key violation. This is obviously because the Shadow database contains no data but still contains foreign keys.
How do I get around this? I have read the articles on data population scripts but they all seem to assume you are deploying to a blank database. Where are you supposed to put maintenance/bug-fix scripts like this?
Tagged:
Answers
This can be done by using the built-in IsShadowDatabase SqlCmd variable. For example:
One caveat to note about this variable is that, in order to avoid false positives occurring in the script generation process, its use should be limited to object types that are not within the scope of the comparison engine. For example, it isn't suitable for DDL statements like CREATE/ALTER. For more information, please see Key Concepts > Script Verification.
Product Manager
Redgate Software
The only other alternative would be to seed the relevant tables referred to in the FK relationship with the required data, e.g. as part of the baseline scripts. However if you've already considered this approach, and subsequently found it to be unworkable, then I'm afraid there's no other solution that the tool offers for this kind of transactional data maintenance at the moment.
I'll take a note to log this in our issue tracking system so that the development team can consider it further. I'll post here again once there is an update.
Product Manager
Redgate Software