Laravel Errno:150“外键约束”是错误形成的。

发布于 2025-01-17 13:06:48 字数 1089 浏览 1 评论 0原文

正在遵循一些不同的教程来尝试学习外键迁移。我有 Laravel v9。

无法解决此错误:

General error: 1005 Can't create table `laravel`.`galleries` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `galleries` add constraint `galleries_album_id_foreign` foreign key (`album_id`) references `albums` (`id`) on delete cascade)

迁移文件:

Schema::create('galleries', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('album_id')->nullable();
    $table->string('title');
    $table->longText('details');
    $table->string('image')->nullable();
    $table->timestamps();
    $table->foreign('album_id')->references('id')->on('albums')
    ->onDelete('cascade');;
});
Schema::create('albums', function (Blueprint $table) {
    $table->id();
    $table->string("name");
    $table->timestamps();
});

我非常感谢您的帮助,因为我不知道该怎么做。

我尝试过: $table->integer('album_id')->unsigned(); ,但没有成功。 确保没有错别字。当我遵循这方面的教程时,一切对我来说看起来都很好。

was following a few different tutorials trying to learn foreign key migration. I have Laravel v9.

Can't solve this error:

General error: 1005 Can't create table `laravel`.`galleries` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `galleries` add constraint `galleries_album_id_foreign` foreign key (`album_id`) references `albums` (`id`) on delete cascade)

Migration file:

Schema::create('galleries', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('album_id')->nullable();
    $table->string('title');
    $table->longText('details');
    $table->string('image')->nullable();
    $table->timestamps();
    $table->foreign('album_id')->references('id')->on('albums')
    ->onDelete('cascade');;
});
Schema::create('albums', function (Blueprint $table) {
    $table->id();
    $table->string("name");
    $table->timestamps();
});

I'd appreciate the help as I have no idea what to do.

I tried: $table->integer('album_id')->unsigned(); which didn't work.
Made sure there's no typo. Everything looks good to me as I was following the tutorial on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的位置 2025-01-24 13:06:48
        $table->bigInteger("album_id")->unsigned();

        $table->foreign('album_id')
        ->references('id')
        ->on('albums')
        ->onDelete('cascade')
        ->onUpdate('cascade');
        $table->bigInteger("album_id")->unsigned();

        $table->foreign('album_id')
        ->references('id')
        ->on('albums')
        ->onDelete('cascade')
        ->onUpdate('cascade');
柳若烟 2025-01-24 13:06:48

对我有用的是,我必须将数据库模式的名称更改为:

Schema::create('galleriesx', function (Blueprint $table) {

现在可以使用。不知道为什么会发生这种情况。如果您能提供帮助,希望有一些见解,谢谢。

What worked for me was, I had to change the name of my database schema as to:

Schema::create('galleriesx', function (Blueprint $table) {

Works now. Have no idea why this happened. Would like some insight if you can help, thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文