是否可以在没有订单的情况下迁移迁移?(如果没有,我该如何解决这个问题)



我描述我的问题的最佳方式是这个链接;

堆栈溢出

的问题正是这样,解决方案实际上正在工作,但就我而言,要么我会有一个替代解决方案,要么我的架构构建器做错了什么,我需要更好地理解它。

我的代码基本上是这样的:

//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');

因此,这两个订单都无法帮助我解决此解决方案。

是否有针对我的情况的解决方案,或者我的代码/架构逻辑错误,我需要修改我的代码?

  • 您的架构不正确。你不能让表相互依赖,即它们不能同时是彼此的主从。这样,您根本无法制作它们。

  • 您应该首先创建主表,例如ABC

架构 A:

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

架构 B:

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

架构 C:

$table->increments('id');
// some other columns
  • 现在,创建子表,换句话说,这些是描述多对多关系的中间表,您可以使用 pivot 属性访问它们。

架构为:

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

架构 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');

架构 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');
  • 现在,您可以按此顺序成功运行迁移,您应该很高兴。

在 Laravel>= 5.0 中,实现此目的的一种方法是将某些脚本放在正确命名的迁移文件夹中。就像我以前在冲刺中进行迁移一样。

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

使用此方法,您必须在每个子文件夹上运行迁移命令:

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

但是,您可以做的是轻松扩展工匠系统以编写您自己的迁移命令,该命令将遍历迁移文件夹下的所有文件夹,为您创建这些命令并运行它们。

你也可以简单地编写一个shell脚本,如果你不想通过工匠来做到这一点

最新更新