Wrong DDL statement order: dropping index before dropping related foreign key constraint - sql

I'm trying to update a target database with SQL Server Data Tools, using the Publish option. I've got both pre-deployment and post-deployment scripts with custom instructions.
Here's the problem: SSDT tries to drop an index numero from my target that doesn't exist in my reference schema, but fails because it is being used for foreign key enforcement by constraint fk_numero. This foreign key is being dropped later in the script since there's another change to be made on this table.
I have considered dropping fk_numero in my pre-deploy script, but it would fail anyway because of the DROP CONSTRAINT fk_numero that is called later in the generated script: since SSDT doesn't write IF EXISTS tests before dropping a constraint, it fails when trying to delete something that doesn't exist.
I have also tried to disable all foreign keys in my pre-deploy script with a NOCHECK CONSTRAINT ALL, hoping I'd then be able to drop my index, to no avail.
Is there an option in SSDT to specify whether you want it to generate DROP CONSTRAINT scripts? Or an option for instructions order? Or a way to hint to SSDT that it should test whether the constraint exists before trying to drop it?

You could try the "DROP objects in target but not in Project" option.
Are you using custom-made scripts to modify the schema?

I just encountered this issue when working with SSDT. Turns out it's a known bug, which was reported on Microsoft Connect. However, it doesn't seem to have been resolved.
UPDATE:
The May 2015 Update for SSDT includes a fix for this issue.

Related

The check keyword not working in sql management studio

I have two tables tblA and tblB. And a constraint called tblA_tblB_FK is created between these tables. I wanted to update both columns in tables chained with tblA_tblB_FK constraint. While reading different posts I thought the best way is to disable the constraint for a moment and enable again after the update. For that reason I executed these queries:
alter table tblA NOCHECK CONSTRAINT tblA_tblB_FK
After this step I did the update and till now everything was OK, but then I tried to enable again the constraint, so I executed this query:
ALTER TABLE tblA CHECK CONSTRAINT tblA_tblB_FK
and it says command successfully completed. But when I try to make update again it doesn't stop me from doing that, meaning there is a problem with the enabling process. I tried to execute another query:
ALTER TABLE tblA WITH CHECK CHECK CONSTRAINT tblA_tblB_FK
and it doesn't allow me complaining there is tblA_tblB_Fk constraint active. I don't understand why it allows me to make an update, while it doesn't allow me to execute this command?
I am using SQL Server 2005. Thanks in advance for any suggestions!
Check you insert and update specification for the foreign key in management studio under Table>Table_name>Keys folder. It might be set to "Cascade".

Drop table with cascade constraint

How do I configure Liquidbase to add cascadeConstraints=true to my diff file when I execute liquibase diffChangeLog? There are drop table commands, but they fail due to other tables having FK on them.
When Liquibase does a diffChangeLog, it tries to do the best that it can, but in your case it won't be able to do what you want. I think your best course of action would be to add the cascadeConstraints attributes manually.

How Do I Ignore Errors When Deleting Records

I'm in the process of testing a large number of schema changes to upgrade our db to run with the latest version of a packaged product we have.
At this point I'm not interested in the data contained in the db, only the schema (i.e. the tables, views, constraints, keys, stored procedures, etc.).
My testing entails running scripts, resolving errors, an re-running the scripts. If I want to re-run the scripts I need to first restore the db to get it back to a known state. Restoring the db is very time consuming as it has lots of data. I would like to "slim down" the db and remove as much data as possible. That way it will be quicker to restore the db and re-run my scripts
When I attempt to delete records from many of the tables ("delete from table-name") I run into constraint errors and the command stops.
Is there a way to allow the command to continue and, in effect, delete all the records in the table where there aren't constraint issues? In other words I'd like the command to ignore errors and continue to delete all the records it can.
Any Suggestions would be greatly appreciated.
Byron above makes a good point; you can disable FOREIGN KEY or CHECK constraints with the ALTER TABLE command:
ALTER TABLE TableName NOCHECK CONSTRAINT ALL
Then do your work, and when finished,
ALTER TABLE TableName CHECK CONSTRAINT ALL
to re-enable constraints. Be careful, though: if you leave your data in an inconsistent state that violates constraints once you re-enable them, future updates can fail due to constraint errors.
Sources:
http://weblogs.sqlteam.com/joew/archive/2008/10/01/60719.aspx
http://technet.microsoft.com/en-us/library/ms190273.aspx

In a visual studio database project, after I add new foreign keys to the project how do I stop it from checking the data against those keys?

I added a bunch of foreign keys to the database project when previously there weren't any. In the generated sql file (sql/debug/$projectname.sql) I see a part that starts with
PRINT N'Checking existing data against newly created constraints';
GO
USE [$(DatabaseName)];
GO
ALTER TABLE [dbo].[table1] WITH CHECK CHECK CONSTRAINT [FK_1];
ALTER TABLE [dbo].[table1] WITH CHECK CHECK CONSTRAINT [FK_2];
and on it goes.
How can I stop the database project from generating this section that checks the data against the new constraints? I tried creating the foreign keys using
ALTER TABLE dbo.table1 WITH NOCHECK
ADD CONSTRAINT [FK_1]
FOREIGN KEY (blah)
REFERENCES Table2 (blah2)
but no dice. Any suggestions?
Aha, there's an advanced option called checkNewConstraints in the Database.sqldeployment properties page under the properties folder. That should do the trick.
In VS 2019, you can find it as given in screenshot below. Uncheck the "Script validation for new constraints".
Right click your project and you will find Publish in the context menu. That will open up the Publish Database dialog. Follow 1, 2, and 3 (uncheck this option) and you will be able to get through the validation of existing data in the table for constraint checks.

symfony doctrine build-sql error

I have some big problems with symfony and doctrine at the beginning of a new project. I have created database diagram with mysql workbench, inserted the sql into phpmyadmin and then I've tried symfony doctrine:build-schema to generate the YAML schema.
It generates a wrong schema (relations don't have on delete/on update) and after this I've tried symfony doctrine:build --sql and symfony doctrine:insert-sql
The insert-sql statement generates error (can't create table ... failing query alter table add constraint ....), so I've decided to take a look over the generated sql and I've found out some differences between the sql generated from mysql workbench (which works perfect, including relations) and the sql generated by doctrine.
I'll be short from now: I have to tables, EVENT and FORM and a 1 to n relation (each event may have multiple forms) so the correct constraint (generated with workbench) is
ALTER TABLE `form` ADD CONSTRAINT `fk_form_event1` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
doctrine generated statement is:
ALTER TABLE event ADD CONSTRAINT event_id_form_event_id FOREIGN KEY (id) REFERENCES form(event_id);
It's totally reversed and I am sure here is the error. What should I do? It's also correct like this?
It it broken, I've wrote the schema manually and it works perfect. I didn't want to do that because it was a very large file, but I've also learned from it!
Thank you guys!
To get both visual representation and automatic code generation you can use ORM Designer so you will have everything covered by one tool.