Foreign Key Constraint Conflict

4 posts, 1 answers
  1. EVP
    EVP avatar
    8 posts
    Member since:
    Jul 2013

    Posted 21 Nov 2014 Link to this post

    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
  2. Answer
    Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 25 Nov 2014 Link to this post

    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.
     
  3. DevCraft banner
  4. EVP
    EVP avatar
    8 posts
    Member since:
    Jul 2013

    Posted 27 Nov 2014 in reply to Boris Georgiev Link to this post

    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
  5. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 02 Dec 2014 Link to this post

    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.
     
Back to Top