如果在 Laravel 6 中使用外键的 table 之后列出的引用 table 不能在迁移中使用外键
Can not use Foreign Key in migration if the referenced table listed after the table that use Foreign key in Laravel 6
假设我有 4 个 table 需要迁移,它在我的程序中按这些顺序列出。
2019_11_11_021524_create_device_brands_table.php
2019_11_11_021820_create_device_types_table.php
2019_11_11_025212_create_transactions_ins_table.php
2019_11_11_150443_create_device_statuses_table.php
然后我在 tranasctions_ins
中引用所有这 3 个 table
public function up()
{
Schema::create('transactions_ins', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('DeviceType_id')->nullable();
$table->unsignedBigInteger('DeviceBrand_id')->nullable();
$table->unsignedBigInteger('DeviceStatus_id')->nullable();
$table->timestamps();
//Foreign Key
$table->foreign('DeviceType_id')->references('id')->on('device_types');
$table->foreign('DeviceBrand_id')->references('id')->on('device_brands');
$table->foreign('DeviceStatus_id')->references('id')->on('device_statuses');
});
}
当我使用php artisan migrate
迁移table时,出现这个错误
SQLSTATE[HY000]: General error: 1005 Can't create table inventory
.transactions_ins
(errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table transactions_ins
add
constraint transactions_ins_devicestatus_id_foreign
foreign key
(DeviceStatus_id
) references device_statuses
(id
))
但是当我从 transactions_in
迁移中删除 DeviceStatus_id
时 table 错误消失了,我知道它发生了,因为 Laravel 不知道 device_statuses
table 不存在,如果我只删除 transactions_in
迁移 table,我该如何解决这个问题?
编辑您的迁移文件名 2019_11_11_150443_create_device_statuses_table.php.set 在您的 transactions_ins_table
之前创建的名称
2019_11_11_023443_create_device_statuses_table.php
这意味着您的 table 是在您的 transactions_ins_table 之前创建的。
假设我有 4 个 table 需要迁移,它在我的程序中按这些顺序列出。
2019_11_11_021524_create_device_brands_table.php 2019_11_11_021820_create_device_types_table.php 2019_11_11_025212_create_transactions_ins_table.php 2019_11_11_150443_create_device_statuses_table.php
然后我在 tranasctions_ins
public function up()
{
Schema::create('transactions_ins', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('DeviceType_id')->nullable();
$table->unsignedBigInteger('DeviceBrand_id')->nullable();
$table->unsignedBigInteger('DeviceStatus_id')->nullable();
$table->timestamps();
//Foreign Key
$table->foreign('DeviceType_id')->references('id')->on('device_types');
$table->foreign('DeviceBrand_id')->references('id')->on('device_brands');
$table->foreign('DeviceStatus_id')->references('id')->on('device_statuses');
});
}
当我使用php artisan migrate
迁移table时,出现这个错误
SQLSTATE[HY000]: General error: 1005 Can't create table
inventory
.transactions_ins
(errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tabletransactions_ins
add constrainttransactions_ins_devicestatus_id_foreign
foreign key (DeviceStatus_id
) referencesdevice_statuses
(id
))
但是当我从 transactions_in
迁移中删除 DeviceStatus_id
时 table 错误消失了,我知道它发生了,因为 Laravel 不知道 device_statuses
table 不存在,如果我只删除 transactions_in
迁移 table,我该如何解决这个问题?
编辑您的迁移文件名 2019_11_11_150443_create_device_statuses_table.php.set 在您的 transactions_ins_table
之前创建的名称2019_11_11_023443_create_device_statuses_table.php
这意味着您的 table 是在您的 transactions_ins_table 之前创建的。