My problem. I have a database and it is a big one, I want to remove a specific constraint between to table and i wanna do it with a migration scripts written in Visual Studio. I have it for development purposes running locally on my pc but it also runs on a staging server. I could use the name of the constraint locally and it would work, but on the staging server the name is different. So thats why I want to do without knowing its name.
I've been reading a lot post here on stackoverflow regarding same issue but none of them works for me.
I've build a small databaseto try different code on before i try it on the big DB.
It looks like this:
I've tried this:
SELECT *
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('Genres')
SELECT
'ALTER TABLE ' + OBJECT_SCHEMA_NAME(parent_object_id) +
'.[' + OBJECT_NAME(parent_object_id) +
'] DROP FOREIGN_KEY_CONSTRAINT ' + name
FROM sys.foreign_keys
WHERE referenced_object_id = object_id('Genres')
And this
DECLARE @SQL NVARCHAR(MAX) = N'';
SELECT @SQL += N'
ALTER TABLE ' + OBJECT_NAME(PARENT_OBJECT_ID) + ' DROP CONSTRAINT ' + OBJECT_NAME(OBJECT_ID) + ';'
FROM SYS.OBJECTS
WHERE TYPE_DESC LIKE '%CONSTRAINT' AND OBJECT_NAME(PARENT_OBJECT_ID) = 'Albums';
EXECUTE @SQL
In both cases it finds the constraint tries to drop it, but is still there. It even prints out the name of the constraint i want to remove.