Question

Apologies for this example, it's the best I could come up with.

The Scenario

I am building a Laravel 4.1 application and I have 2 tables, people and houses in a database called hogwarts.

people includes students, staff, ghosts, mascots and so on, so there is a foreign key of house_id relating each and every person to a house. Every house has a head of house, so there is a head_id linking the house back to a person.

This creates a loop of foreign keys preventing each table from being seeded until the other is seeded, so what I chose to do was to allow the head_id on the houses table to be nullable.

This means that I can seed the houses table first, with no heads of houses set, then seed the people table with each person related to their house.

The Problem

The problem is that I now need to specify another seed to alter the houses table in order to set the head_id for each house, but I cannot find anywhere that tells you how to update existing data within a seed.

Was it helpful?

Solution

The database structure you describe creates a circular dependency causing the issue you describe, there is nothing fundamentally wrong with the circular dependency in itself, but it may cause constraint violations when interacting with the data from outside of the DBMS.

I would therefore suggest adding a third table to your database to abstract the relationship between the people and the houses which perhaps contains a boolean field indicating whether a person is head of house or not.

You can then add unique constraints to the new table:

one across house_id and the new boolean field to ensure there is only one head per house

UPDATE: The above constraint won't work because (house_id, true) will be unique once, but then you'll have duplication with many (house_id, false) values.

If you leave the head_id in the house table then you can seed the people table first, and when you seed the house table you can add the id of the head person without violating the relationship as it's no longer circular.

You can then seed the third table to relate people to houses solving the circular dependency issue.

You can add a unique constraint on person_id in the new table to ensure a person can only be a member of one house or alternatively you can take care of these rules in your business logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top