What to do when relation cannot be created because their is violating data in the referenced table

Relation cannot be created because their is violating data in the referenced table

Are you trying to join two tables together & you get the error message, "relation cannot be created because their is violating data in the referenced table"?

In this article we are going to take a look:

  • Why this error message flags up

  • How you can solve the error message

  • How to continue to create a relationships between your different tables.

Why the error?

This error occurs when you have two tables that you're trying to join together & both or one of these tables already has data in it. For example, look at the two tables below.

Imagine that the rentals table & the users table in figure 1.1 do not have a relationship. If you wanted to say that each Rental was owned by a users, you might decide to draw a relationship between the user_id field in the Rentals table & the id field in the Users table. As shown below.

This relation is not allowed, & Dittofi will alert you with the following error message.

The reason there is an error is because there is already data in the Rentals table has a user_id that does not have a corresponding id in the users table. This is the property with a user_id of 14.

How to solve the error message

The example in figure 1.1 is quite basic. In this case you could:

  • Change the user_id 14 to another value that is in the users table

  • Delete the record with the user_id 14.

  • Add a at least 14 users to the Users table.

In cases where you have a lot of data, you can build a small action to update your data from right inside the Dittofi platform. For instance, you could:

  1. Create an action to loop through all records in your table.

  2. Use a has_record event to check if related data existed in another table.

  3. If the data didn't exist, then you will update the record or delete it.

Last updated