Php 如果在Laravel 6中使用外键的表之后列出了引用的表,则无法在迁移中使用外键
假设我有4个表要迁移,在我的程序中按这些顺序列出 2019_11_11_021524_创建_设备_品牌_table.php 2019_11_11_021820_创建_设备_类型_table.php 2019_11_11_025212_创建_事务_ins_table.php 2019_11_11_150443_创建_设备_状态_table.php 然后我引用了Php 如果在Laravel 6中使用外键的表之后列出了引用的表,则无法在迁移中使用外键,php,mysql,laravel,laravel-6,Php,Mysql,Laravel,Laravel 6,假设我有4个表要迁移,在我的程序中按这些顺序列出 2019_11_11_021524_创建_设备_品牌_table.php 2019_11_11_021820_创建_设备_类型_table.php 2019_11_11_025212_创建_事务_ins_table.php 2019_11_11_150443_创建_设备_状态_table.php 然后我引用了transasctions\u ins public function up() { Schema::create(
transasctions\u 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
迁移表时,会出现此错误
SQLSTATE[HY000]:一般错误:1005无法创建表清单
事务\u-ins
(错误号:150”外键约束为
格式不正确”)(SQL:alter tabletransactions\u-ins
add
约束transactions\u ins\u devicestatus\u id\u foreign
外键
(DeviceStatus\u id
)参考device\u状态
(id
)
但是当我从迁移表中的
transactions\u中删除DeviceStatus\u id
时,错误消失了,我知道它发生了,因为Laravel不知道设备状态
表不存在,如何解决这个问题?我应该删除迁移表中的事务吗?编辑迁移文件名2019\u 11\u 150443\u创建\u设备\u状态\u table.php。设置在事务\u表之前创建的名称
2019_11_11_023443_create_device_statuses_table.php
这意味着您的表是在事务表之前创建的