gpt4 book ai didi

mysql - Laravel 6 外键约束格式不正确

转载 作者:行者123 更新时间:2023-11-29 15:19:20 25 4
gpt4 key购买 nike

我有 2 个表,当我尝试迁移时返回此错误:

General error: 1005 Can't create table usee_anbari.#sql-473_21177 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table companies add constraint companies_access_id_foreign foreign key (access_id) references accesses (id) on delete cascade)

这是我的表格:

public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->text('address');
$table->string('tel1');
$table->string('tel2');
$table->integer('owner');
$table->unsignedBigInteger('access_id');
$table->string('depot_number')->default(2);
$table->timestamps();


$table->foreign('access_id')->references('id')->on('accesses')
->onDelete('cascade');
});
}

还有一个:

public function up()
{
Schema::create('accesses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('type');
$table->string('description');
$table->timestamps();
});
}

我想念什么?

最佳答案

在您的 database/migrations 文件夹中,按名称排序。然后确保 create_accesses_table 位于 create_companies_table 之前:

enter image description here

关于mysql - Laravel 6 外键约束格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59472945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com