-1

Best way for me to describe my problem and it's go-to solution would be this link;

StackOverflow

My problem is exactly this, and the solution actually is working, but not in my case, either I will have an alternative solution for mine, or I'm doing something wrong with my schema builder and I need to understand it better.

My code is basically like this:

//just an example, not my code

Schema A (as)

//other code, such as table->increments('id')

$table->unsignedInteger('b_id');
$table->unsignedInteger('c_id');

$table->foreign('b_id')->references('id')->on('bs'); 
$table->foreign('c_id')->references('id')->on('cs');


Schema B (bs)

$table->unsignedInteger('a_id');
$table->unsignedInteger('c_id');

$table->foreign('a_id')->references('id')->on('as'); 
$table->foreign('c_id')->references('id')->on('cs');


Schema C (cs)

$table->unsignedInteger('a_id');
$table->unsignedInteger('b_id');

$table->foreign('a_id')->references('id')->on('as'); 
$table->foreign('b_id')->references('id')->on('bs');

So neither order helps me with this solution.

Is there a solution to my case, or my code/schema logic is wrong and I need to modify my code?

maltoshi
  • 87
  • 8
  • Please edit your question and add how you are creating your schema, we can't help with this information. –  Feb 01 '19 at 10:52
  • Laravel migrations are run from the oldest to the newest. You need to logically re-arrange your migrations files. I cant understand why userSchema references both the other tables. Can you please post your migrations files? – pmarkoulidakis Feb 01 '19 at 10:53
  • I edited my post, it's still not my real code, but my real code is very long and the logic is basically the same. Now I'm thinking two schemas referencing each other isn't a good idea, that why I'm struggling? – maltoshi Feb 01 '19 at 11:05

2 Answers2

2
  • Your schema is incorrect. You can't have tables being interdependent, i.e, they can't be both master and slave to each other at the same time. This way, you can never make them at all.

  • You should create master tables first, let's say A,B,C.

Schema A:

$table->increments('id');
// some other columns

Schema B:

$table->increments('id');
// some other columns

Schema C:

$table->increments('id');
// some other columns
  • Now, create the child tables, in other words, these are intermediate tables describing many-to-many relationships and you can access them using pivot attribute.

Schema AS:

$table->unsignedInteger('b_id');
$table->unsignedInteger('c_id');

$table->foreign('b_id')->references('id')->on('B'); 
$table->foreign('c_id')->references('id')->on('C');

Schema BS:

$table->unsignedInteger('a_id');
$table->unsignedInteger('c_id');

$table->foreign('a_id')->references('id')->on('A'); 
$table->foreign('c_id')->references('id')->on('C');

Schema CS:

$table->unsignedInteger('a_id');
$table->unsignedInteger('b_id');

$table->foreign('a_id')->references('id')->on('A'); 
$table->foreign('b_id')->references('id')->on('B');
  • Now, you can successfully run a migration in this order and you should be good to go.
nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

In Laravel >= 5.0, one way to achieve this is to have certain scripts in properly named migration folders. Like I use to have migrations in Sprints.

--migrations/ ---Sprint8/ ------user_table.php ------car_table.php --Sprint9/ ------complaint_table.php ------supervisor_table.php

With this approach, you have to run the migration command on each of your subfolders:

php artisan migrate --path=/database/migrations/Sprint8 php artisan migrate --path=/database/migrations/Sprint9

However, what you can do is easily extend the artisan system to write your own migrate command that will iterate through all folders under the migrations folder, create these commands for you and run them.

You can also simply write a shell script if you don't want to get into doing this via artisan

FirstIndex
  • 159
  • 7