Laravel Errno:150“外键约束”是错误形成的。
正在遵循一些不同的教程来尝试学习外键迁移。我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我有用的是,我必须将数据库模式的名称更改为:
现在可以使用。不知道为什么会发生这种情况。如果您能提供帮助,希望有一些见解,谢谢。
What worked for me was, I had to change the name of my database schema as to:
Works now. Have no idea why this happened. Would like some insight if you can help, thanks.