EF Core migration FK constraint - database

I'm trying to drop my foreign key but I got the error:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_TABLE_ITEM_LIST_DETAILS_TABLE_ITEM1_ITEM_ID". The conflict occurred in database "TABLE", table "dbo.TABLE_ITEM", column 'ID'
I tried to run add-migration and update-database.
How to fix this error?

Related

Migrating a self referencing table using Azure Data Factory

I am trying to migrate a table which has 2 colums.
Id - Primarky key
Parent ID - Foreign key populated by the value above (Id).
(So basically the FK is in the same table)
When I migrate this, I get the following error.
"The UPDATE statement conflicted with the FOREIGN KEY SAME TABLE constraint"
Please let me know how to deal with this.
Thanks in advance
Disable the foreign key constraint first. Then migrate your data. And afterwards you can enable the constraint again.
ALTER TABLE Purchasing.PurchaseOrderHeader
NOCHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
ALTER TABLE Purchasing.PurchaseOrderHeader
CHECK CONSTRAINT FK_PurchaseOrderHeader_Employee_EmployeeID;
Disable foreign key constraints with INSERT and UPDATE statements

Cannot add cascade on update on two column of table referencing to same table same column

I'm using SQL Server.
I have one table named PROGRAM_MASTER which has two columns created_by and updated_by which are referencing to USERS table on column id.
Now I want to define cascade on update option to both these columns using below SQL command.
alter table program_master add constraint program_master_created_by_foreign foreign key (created_by) references users (id) on update cascade
alter table program_master add constraint program_master_upated_by_foreign foreign key (updated_by) references users (id) on update cascade
But foreign key constraint gets added to created_by, but while creating one for updated_by I get following error.
SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Introducing FOREIGN KEY constraint
'program_master_updated_by_foreign' on table 'program_master' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
How should I resolve this error?

Delete data with SQL constraints

I have the following:
DELETE FROM ContactBase
DELETE FROM AccountBase
It errors with:
The DELETE statement conflicted with the REFERENCE constraint "account_primary_contact". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.AccountBase", column 'PrimaryContactId'.
The DELETE statement conflicted with the REFERENCE constraint "account_contacts". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.ContactBase", column 'AccountId'.
I understand because of the constraint I need to delete the data in a particular order, but if I reverse it:
DELETE FROM AccountBase
DELETE FROM ContactBase
It just reverses the error messages:
The DELETE statement conflicted with the REFERENCE constraint "account_contacts". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.ContactBase", column 'AccountId'.
The DELETE statement conflicted with the REFERENCE constraint "account_primary_contact". The conflict occurred in database "BMBLANK_MSCRM", table "dbo.AccountBase", column 'PrimaryContactId'.
How do I empty these tables?
Thanks
First drop the constraints , delete the data and add them again:
ALTER TABLE AccountBase
DROP CONSTRAINT account_contacts;
ALTER TABLE ContactBase
DROP CONSTRAINT account_primary_contact;
DELETE FROM ContactBase;
DELETE FROM AccountBase;
ALTER TABLE AccountBase
ADD FOREIGN KEY (account_contacts)
REFERENCES ContactBase(PrimaryContactId);
ALTER TABLE ContactBase
ADD FOREIGN KEY (account_primary_contact)
REFERENCES AccountBase(AccountId);
Maybe I mixed them, its confusing without the tables DDL's , so if I did just adjust it.
You can generate a script of the constraints, drop the constraints, delete from tables, then use the script to recreate the constraints.
How to generate all constraints scripts
Drop the constraints and then delete the data from the tables. Then, if needed, add the constraints again.

Delete foreign key with primary key reference

I tried many forums but was not satisfied - I have a table that has a primary key and foreign key relation.
I have to delete table rows with primary key so I need to remove the constraints before deleting.
I used:
delete from [docd_metadata].[docd_metadata].[STATEMENT_IMAGES]
where [statement_image_id]= 05291520275
I got error:
The DELETE statement conflicted with the REFERENCE constraint "fk_stmnt_image_StmntImageId". The conflict occurred in database "docd_metadata", table "docd_metadata.STATEMENT_CAMPAIGN", column 'STATEMENT_IMAGE_ID'.
so I tried :
ALTER TABLE[docd_metadata].[docd_metadata].[STATEMENT_IMAGES]
DROP CONSTRAINT [fk_stmnt_image_StmntImageId]
Now I am getting :
Constraint 'fk_stmnt_image_StmntImageId' does not belong to table 'STATEMENT_IMAGES'
Schema:
Also:
Any suggestion please?
If you really closely read the error message, it's clear that the FK constraint is on table docd_metadata.STATEMENT_CAMPAIGN and not on STATEMENT_IMAGES - so therefore, you must use this SQL to drop the FK constraint:
ALTER TABLE [docd_metadata].[STATEMENT_CAMPAIGN]
DROP CONSTRAINT [fk_stmnt_image_StmntImageId]
The FK goes from table [docd_metadata].[STATEMENT_CAMPAIGN] (column STATEMENT_IMAGE_ID) to [docd_metadata].[STATEMENT_IMAGES] - one table has the primary key, which another table references via its foreign key.

Update even though using constraints

I'm trying to update a value (small spelling error) in the SQL Server 2014 database.
I do like this:
update t_Table set funID = 'References' where funID = 'Referencies'
When doing this I get the error
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_t_Table_Language_t_Table". The conflict occurred in database "db", table "dbo.t_Table".
If updating the foreign key, I get a similar error.
Is there any way to update all values at the same time?
I have a vague memory of someone showing a way to do this in management studio but I do not recall how.
Enable update cascade on the foreign key
ALTER TABLE t_Table_Language DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table_Language ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table(funID)
ON UPDATE CASCADE
EDIT:
Or the other way around, i'm not sure which table has the foreing key
ALTER TABLE t_Table DROP CONSTRAINT FK_t_Table_Language_t_Table
ALTER TABLE t_Table ADD CONSTRAINT FK_t_Table_Language_t_Table
FOREIGN KEY (funID) REFERENCES t_Table_Language(funID)
ON UPDATE CASCADE
Then do the update on the referenced table (the master table).
UPDATE t_Table_Language
SET funID = 'References'
WHERE funID = 'Referencies'

Resources