Jaq*_*arh 1 php database-migration laravel laravel-5 laravel-artisan
我正在尝试迁移我新创建的迁移。问题是等待迁移的一批迁移是按照我使用创建迁移的顺序
php artisan make:migration
Run Code Online (Sandbox Code Playgroud)
因此,我的product_list
迁移试图在尚未迁移的表上设置外键。
$table->foreign('product_category')->references('id')->on('product_categories');
$table->foreign('product_type')->references('id')->on('product_types')->onDelete('cascade');
Run Code Online (Sandbox Code Playgroud)
当我运行时,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.
Run Code Online (Sandbox Code Playgroud)
是否可以在我的表之前迁移我的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 | |
+------+------------------------------------------------+-------+
Run Code Online (Sandbox Code Playgroud)
我希望它按顺序迁移
| 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 | |
+------+------------------------------------------------+-------+
Run Code Online (Sandbox Code Playgroud)