gpt4 book ai didi

laravel - 一般错误 : 1005 Can't create table , Laravel 中外键约束的形成不正确

转载 作者:行者123 更新时间:2023-12-02 16:15:10 25 4
gpt4 key购买 nike

为什么 Laravel 模式回复

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1005 Can't create table test.#sql-13cc_d0 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table cities add constrai nt cities_provinces_id_foreign foreign key (provinces_id) references provinces (id) on delete cascade)

[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table test.#sql-13cc_d0 (errno: 150 "Foreign key constraint is incorrectly formed")

第一个表

Schema::create('provinces', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();
});

第二个表

    Schema::create('cities', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('province_id')->unsigned()->index();
$table->boolean('is_enable');
$table->boolean('is_deletable');
$table->boolean('is_editable');
$table->boolean('deleted');
$table->timestamps();


$table->foreign('province_id')
->references('id')->on('provinces')
->onDelete('cascade');

});

最佳答案

您应该确保您的 provinces 表迁移在 cities 表迁移之前运行。

您应该更改 provincescities 迁移文件名,以确保 provinces 表迁移文件开头的时间戳位于 城市表。

关于laravel - 一般错误 : 1005 Can't create table , Laravel 中外键约束的形成不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41259651/

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