php - 如何创建数据透视表 laravel?
问题描述
我有一个简单的博客,其中有一个包含标题和标签的帖子,现在我希望用户能够添加标签,根据我需要使用名称约定创建数据透视表的文档。
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`))
我的代码做错了什么?
解决方案
改成$table->integer('post_id')->unsigned();
在$table->bigInteger('post_id')->unsigned();
你tag_id
也。
推荐阅读
- r - 如何使用多项 logit 模型的标准误差获得平均边际效应 (AME)?
- c# - 如何将 Base64 编码的字符串直接转换为 SecureString
- matlab - 计算数组中的重复整数
- static-analysis - cppcheck 配置检查是否释放了不属于自己的内存
- c++ - OSX 升级权限
- c# - EF Core、Fluent API 和关系:生成错误 SQL 的问题
- variables - 如何链接变量,所以当我修改其中一个时,它们都被修改了?或者只是顶级的
- java - LocationManager GPS_PROVIDER 花费了太多时间
- node.js - Zoho 邮件在 Node Js 中显示 535 Authentication Failed
- php - PHP:接收和存储大型二进制数据