【发布时间】:2019-11-21 20:58:46
【问题描述】:
当我尝试运行迁移以创建外键约束时,我在命令提示符中收到以下错误。我需要帮助来克服它,因为我在互联网上进行了很多搜索并尝试了许多以前相同类型问题的答案,不幸的是,没有一个有效
Illuminate\Database\QueryException : SQLSTATE[HY000]: 一般错误: 1005 无法创建表
db_blog.category_posts(errno: 150 "外键约束格式不正确") (SQL: alter table @987654323 @添加约束category_posts_post_id_foreign外键(post_id)在删除级联上引用posts(id)在 1PDOException::("SQLSTATE[HY000]: 一般错误: 1005 无法创建表
db_blog.category_posts(errno: 150 "外键约束格式不正确")")
请使用参数 -v 查看更多详细信息。
public function up()
{
Schema::create('category_posts', function (Blueprint $table) {
$table->integer('post_id')->unsigned()->index();
$table->integer('category_id')->unsigned()->index();
$table->foreign('post_id')->references('id')->on('posts')-
>onDelete('cascade');
$table->timestamps();
});
}
不明白这方面哪里有问题
【问题讨论】:
-
posts.id是什么类型的列?在后表中。 -
在帖子表中帖子 ID 类型为“bigint”
-
那么
post_id中的category_posts也必须属于bigint。列的类型需要匹配。 -
我尝试在 laravel 中使用“整数”类型和“增量”,但不起作用
-
如果
posts.id是bigint(签名),那么category_posts.post_id必须是bigint(签名)。所以它们必须是相同的符号、长度和类型。完全一样。
标签: php mysql database laravel