This is a migrated thread and some comments may be shown as answers.
Foreign Key Constraint Conflict
3 Answers 19 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
EVP
Top achievements
Rank 2
EVP asked on 21 Nov 2014, 11:03 PM
Hi there,
                I am getting a "The INSERT statement conflicted with the FOREIGN KEY constraint " exception when trying to just throw some dummy data into a database for testing. The main object I am trying to create (insert a record for) is an Account. There is also a ForgotPassword object, which has an association (1:1) with Account, however, I don't want to be required to create a ForgotPassword object in order to be able to create an account object. Should I not have an association between these two objects to accomplish this?

Additional information: Insert of '146059497-04ac8908-682b-4544-8332-a7bfa4d41f7a' failed: Telerik.OpenAccess.RT.sql.SQLException: The INSERT statement conflicted with the FOREIGN KEY constraint "ref_Account_ForgotPassword". The conflict occurred in database "horizon", table "dbo.ForgotPassword", column 'forgot_id'.

Thanks much,

Eric

3 Answers, 1 is accepted

Sort by
0
Accepted
Boris Georgiev
Telerik team
answered on 25 Nov 2014, 04:06 PM
Hello Eric,

It looks like the relationship in the database is reversed, which means that Primary Key table in the 1:1 association is the ForgetPassword table, that is why when you try to insert a record in the Account table, the server throws Foreign Key violation because in the ForgetPassword table there is no entity with the Id of the new Account entity.

If you revers the relationship in the SQL server, so the Account table to be the Primary Key table and then the association created in the Domain Model, everything should be fine. If you are in Model First scenario, you should first reverse the association in the Domain Model and then Upgrade the Database.

I hope that helps. If any other questions arise, do not hesitate to contact us again.

Regards,
Boris Georgiev
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
EVP
Top achievements
Rank 2
answered on 28 Nov 2014, 02:40 AM
Hi Boris,
          
      Thanks for your reply. That fixed it! However, I noticed that after modifying the associations, I could just right click > update database from model, as it didn't detect any changes to update. I had to drop the tables in the database w/ the constraints, and then it detected to add those back, with the updated associations. Is there another way I should have tried to push those association updates to the database, without having to drop tables?

Thanks!

Eric
0
Boris Georgiev
Telerik team
answered on 02 Dec 2014, 04:29 PM
Hello Eric,

It is not necessarily to drop the tables, you could drop only the constraint and execute Update Database from Model to generate the DDL Script. There are two ways to achieve that, from the database drop the constraint or from Model Schema Explorer, expand the Constraints node, find the constraint and delete it from there.

I hope that helps.

Regards,
Boris Georgiev
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
General Discussions
Asked by
EVP
Top achievements
Rank 2
Answers by
Boris Georgiev
Telerik team
EVP
Top achievements
Rank 2
Share this question
or