是否可以更改表在 Laravel 中的迁移顺序?
Is it possible to change the order in which tables migrate in Laravel?
我正在尝试迁移我新创建的迁移。问题是等待迁移的那批迁移是按照我使用
创建迁移的顺序排列的
php artisan make:migration
因此,我的 product_list
迁移试图在尚未迁移的 table 上设置外键。
$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
table 之前迁移我的 product_category
和 product_types
table?这样我就可以不出错地设置外键了。
我的迁移状态如下所示:
+------+------------------------------------------------+-------+
| 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 | |
+------+------------------------------------------------+-------+
您可以做的最简单的事情是在文件名上排序日期和时间,最后是 laravel 以按顺序迁移数据库。
将迁移文件的名称更改为比您希望运行更早的日期时间
所以将文件名改为例如
2019_01_15_00000_product_categories
手动更改时间戳,这样您要首先迁移的文件的文件名应该比您要稍后迁移的文件更早timestamp/date。
我正在尝试迁移我新创建的迁移。问题是等待迁移的那批迁移是按照我使用
创建迁移的顺序排列的php artisan make:migration
因此,我的 product_list
迁移试图在尚未迁移的 table 上设置外键。
$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
table 之前迁移我的 product_category
和 product_types
table?这样我就可以不出错地设置外键了。
我的迁移状态如下所示:
+------+------------------------------------------------+-------+
| 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 | |
+------+------------------------------------------------+-------+
您可以做的最简单的事情是在文件名上排序日期和时间,最后是 laravel 以按顺序迁移数据库。
将迁移文件的名称更改为比您希望运行更早的日期时间
所以将文件名改为例如
2019_01_15_00000_product_categories
手动更改时间戳,这样您要首先迁移的文件的文件名应该比您要稍后迁移的文件更早timestamp/date。