我有一个简单的博客,其中有一个包含标题和标签的帖子,现在我希望用户能够根据使用名称约定创建数据透视表所需的文档添加标签。
PHPMyAdmin上的帖子表
id title
1 Death
2 Love
PHPMyAdmin上的标签表
id name
这是我创建数据透视表的迁移
<?php
class CreatePostTagTable extends Migration
{
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->references('id')->on('tags');
});
}
public function down()
{
Schema::dropIfExists('post_tag');
}
}
现在,当我运行PHP artisan migrate
时,出现以下错误。
SQLSTATE[HY000]: General error: 1005 Can't create table `royalad`.`#sql-ee8_307` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `post_tag` add constraint `post_tag_post_id_foreign` foreign key (`post_id`) references `posts` (`id`))
我的代码在做什么错?
分析解答
也将tag_id
中的$table->integer('post_id')->unsigned();
更改为$table->bigInteger('post_id')->unsigned();
。