【问题标题】:Is it possible to change the order in which tables migrate in Laravel?是否可以更改 Laravel 中表迁移的顺序?
【发布时间】:2019-06-09 07:45:08
【问题描述】:

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

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_list 表之前迁移我的product_category 和product_types 表?这样我就可以正确设置外键了。

我的迁移状态如下所示:

+------+------------------------------------------------+-------+
| 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                 |       |
+------+------------------------------------------------+-------+

【问题讨论】:

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


    【解决方案1】:

    您可以做的最简单的事情是在文件名上排序日期和时间,最终由 laravel 依次迁移数据库。

    【讨论】:

    • 所以文件名的最后 6 位数字有变化吗?我不需要运行任何缓存清除命令,只需按时间顺序更新最后 6 位我想先迁移到最后并重新运行 php artisan migrate ?谢谢你
    • 是的,如果可能的话,加上日期。
    • 太棒了,现在可以正确迁移。但是,它实际上并没有解决外键错误。 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table iezonsolutions.#sql-1844_178 (errno: 150 "Foreign key constraint is incorrectly formed")")
    • Schema::create('product_categories', function (Blueprint $table) { $table->increments('id'); ... }); 我的 product_categories 是这样的
    • 在我看来,您忘记在模式中添加密钥。首先添加键,然后添加约束。例如。 $table->integer('product_category');然后添加约束线
    【解决方案2】:

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

    Laravel change migration order

    所以把文件名改成example

    2019_01_15_00000_product_categories
    

    【讨论】:

    • 感谢您的链接和示例!
    【解决方案3】:

    手动更改时间戳,以便您首先要迁移的文件的文件名应比您稍后要迁移的文件更早的时间戳/日期名称。

    Changing Migration Order

    【讨论】:

      猜你喜欢
      • 2015-12-08
      • 2015-10-09
      • 2017-11-24
      • 1970-01-01
      • 2017-03-17
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      相关资源
      最近更新 更多