是否可以更改表在Laravel中的迁移顺序?

时间:2019-01-15 09:54:49

标签: php laravel laravel-5 database-migration artisan

我正在尝试迁移新创建的迁移。问题是等待迁移的迁移批次是按照我使用创建迁移的顺序

php artisan make:migration

因此,我的product_list迁移正在尝试在尚未迁移的表上设置外键。

$table->foreign('product_category')->references('id')->on('product_categories');
$table->foreign('product_type')->references('id')->on('product_types')->onDelete('cascade');

运行php artisan migrate时出现以下错误:

C:\xampp\htdocs\iezonsolutions>php artisan migrate
Migrating: 2019_01_15_001617_product_list

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `iezonsolutions`.`#sql-1844_16e` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `product_list` add constraint `product_list_product_category_foreign` foreign key (`product_category`) references `product_categories` (`id`))

  at C:\xampp\htdocs\iezonsolutions\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `iezonsolutions`.`#sql-1844_16e` (errno: 150 "Foreign key constraint is incorrectly formed")")
      C:\xampp\htdocs\iezonsolutions\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\iezonsolutions\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  Please use the argument -v to see more details.

是否可以将product_category表和product_types表迁移到product_list表之前?然后,我将能够正确设置外键。

我的迁移状态如下:

+------+------------------------------------------------+-------+
| Ran? | Migration                                      | Batch |
+------+------------------------------------------------+-------+
| Yes  | 2014_10_12_000000_create_users_table           | 1     |
| Yes  | 2014_10_12_100000_create_password_resets_table | 1     |
| Yes  | 2019_01_14_203800_server_mode                  | 2     |
| No   | 2019_01_15_001617_product_list                 |       |
| No   | 2019_01_15_002318_product_types                |       |
| No   | 2019_01_15_002336_product_categories           |       |
| No   | 2019_01_15_002357_product_skus                 |       |
+------+------------------------------------------------+-------+

我希望它按顺序迁移

| No   | 2019_01_15_002336_product_categories           |       |
| No   | 2019_01_15_002318_product_types                |       |
| No   | 2019_01_15_001617_product_list                 |       |
| No   | 2019_01_15_002357_product_skus                 |       |
+------+------------------------------------------------+-------+

3 个答案:

答案 0 :(得分:4)

最简单的方法是在文件名上排序日期和时间,然后laravel依次跟随日期和时间依次迁移数据库。

答案 1 :(得分:2)

将迁移文件的名称更改为比您希望之前运行的文件时间更短的日期时间

Laravel change migration order

因此将文件名更改为例如

2019_01_15_00000_product_categories

答案 2 :(得分:0)

手动更改时间戳,以使首先要迁移的文件的时间戳/日期名称早于以后要迁移的文件。

Changing Migration Order