Php 外键约束格式不正确-Laravel
我在XAMPP堆栈上的Laravel 5.8上Php 外键约束格式不正确-Laravel,php,mysql,laravel,Php,Mysql,Laravel,我在XAMPP堆栈上的Laravel 5.8上 考虑以下两种迁移以创建两个表: post_类别表 Schema::create('post_categories', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('slug'); $table->string('status'); $table->
考虑以下两种迁移以创建两个表: post_类别表
Schema::create('post_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->string('status');
$table->timestamps();
});
员额表
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('slug');
$table->mediumText('description');
$table->integer('views');
$table->integer('post_category_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('post_category_id')->references('id')->on('post_categories');
});
运行php artisan migrate
时,出现以下错误:
一般错误:1005无法创建表testdb
\sql-38d8_102
(错误号:150“外键约束格式不正确”)
我试过两种方法:
php artisan迁移
php-artisan-migrate:fresh
post\u category\u id
字段改为常规整数,而不是无符号:无差异
也重新启动了MySQL服务器和Apache服务,没有什么不同
post\u类别
迁移在posts
迁移之前运行。将创建posts
表,但外键不创建
为什么会抛出此错误以及如何解决它?这可能是因为您试图创建一个外键,其中包含一个整数字段到一个大整数字段。在您的帖子中,迁移,而不是这个
$table->integer('post_category_id')->unsigned();
这样做:
$table->bigInteger('post_category_id')->unsigned();
与此相反:
$table->integer('post_category_id')->unsigned();
试试这个:
$table->unsignedBigInteger('post_category_id');
我有同样的错误,我想链接两个表,用户表和存款表。我尝试了很多选择,但都不起作用。然而,这正是我在创建存款迁移方面所做的工作:
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
检查迁移的顺序。在这种情况下,“post_categories”应该放在“posts”之前。。。最后,在下一次迁移中尝试添加外键,同时保持引用的id类型相同……我的评论与您的问题无关,但您忘记了引用您计算机上的
user\u id
FKmigration@LucasPiazzi,是的,我已经添加了它,谢谢。这解决了它,我甚至没有注意到bigIncrements字段。它过去只是在老的Laravel版本中的增量,无论如何,谢谢!