Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to enable constraints. When using a data table adapter

I'm getting

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

for the following query in my table adapter.

select 
f.id, f.name, p.productid, p.masterproductID, f.productid, f.dateCreated, f.retired, p.code as HTML_DisplayName, p.ThumbnailID, p.code
from FormSaveData f 

inner join Products p on (f.productid = p.ProductID or f.productId = p.MasterProductID)

where f.userId = 130559
and (p.b_IsArchived = 0 and p.b_IsRetired=0 and p.b_IsStaged = 0)

When I run the query on its own, it works just fine, other userIds also work just fine, so this is a very particular case. I've narrowed it down to the fact that I am inner joining on

f.productid = p.ProductID **or** f.productId = p.MasterProductID

I believe in certain rare cases this causes some kind of conflict that the table adapter does not like. I'm pretty much a novice when it comes to table adapters and SQL so any advice on how this can be better written or why this is happening would be greatly appreciated.

There are no constraints, keys or special rules set for anything in the FormSaveData table.

like image 846
Maxx Avatar asked Oct 14 '11 22:10

Maxx


People also ask

How do you add constraints to an existing table syntax?

ADD CONSTRAINT is a SQL command that is used together with ALTER TABLE to add constraints (such as a primary key or foreign key) to an existing table in a SQL database. The basic syntax of ADD CONSTRAINT is: ALTER TABLE table_name ADD CONSTRAINT PRIMARY KEY (col1, col2);

Which of the below constraints restricts the user from entering invalid data?

NOT NULL Constraint It restricts a column from having a NULL value.


1 Answers

Looks to me that your query is returning several records with the same product_id and the table adapter expects only unique rows. I am pretty sure you can disable the behavior by setting EnforceConstraints to false.

like image 66
Icarus Avatar answered Nov 15 '22 22:11

Icarus