Laravel外键约束格式不正确,我已经搜索过,找不到答案


class CreateMediaTable extends Migration
{
public function up()
{
Schema::create('media', function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('id_users');
$table->unsignedBigInteger('id_posts');
$table->char('type', 1); //P: photo or V: video
$table->string('file');
$table->timestamps();
$table->foreign('id_posts')->references('id')->on('posts');
$table->foreign('id_users')->references('id')->on('users');
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}

和我的创建配置文件迁移

/**
* @author Alex Madsen
* 
* @date November 6, 2018
*/ 
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateUserProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_profiles', function (Blueprint $table) {
$table->unsignedInteger('id_users')->unique();
$table->string('handle')->unique(); 
$table->string('icon')->default('https://i.redd.it/130am13nj6201.png'); 
$table->string('profile_image')->nullable(); 
$table->string('description')->nullable(); 
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_profiles');
}
}

不断抛出这些错误,我错过了什么?请注意,我是新手,试图在业余时间与 youtube 和堆栈溢出一起学习。不知道该走哪条路。我在论坛上查看并尝试了$table->外国('id_posts'(->参考('id'(->on('帖子'(;但它并没有解决问题。

IlluminateDatabaseQueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `ci`.`media` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter t
able `media` add constraint `media_id_posts_foreign` foreign key (`id_posts`) references `posts` (`id`))
at C:xampp6htdocslolsimple_social_network_laravelvendorlaravelframeworksrcIlluminateDatabaseConnection.php:664
660|         // If an exception occurs when attempting to run a query, we'll format the error
661|         // message to include the bindings with SQL, which will make this exception a
662|         // lot more helpful to the developer instead of just the database's errors.
663|         catch (Exception $e) {
> 664|             throw new QueryException(
665|                 $query, $this->prepareBindings($bindings), $e
666|             );
667|         } 
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('id_users');
$table->unsignedInteger('subreddit_id');
$table->text('description');
$table->string('title');
$table->text('content');
$table->unsignedInteger('id_posts');
$table->timestamps();
$table->foreign('id_users')->references('id')->on('users');
$table->foreign('subreddit_id')->references('id')->on('id_posts');
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}

使用外键时,类型应完全相同。

您创建:

$table->unsignedBigInteger('id_posts');

所以它是无符号的大整数,但可能posts表中而不是bigIncrements你只对id列使用increments,这就是你收到此错误的原因。

所以很有可能代替:

$table->unsignedBigInteger('id_posts'); 

你应该使用

$table->unsignedInteger('id_posts'); 

或作为替代解决方案使用

$table->bigIncrements('id');

posts迁移

posts迁移中,您使用increments进行了id这意味着它需要

unsigned integer auto_increment

但是在迁移media文件时,您创建了一个posts_id具有unsignedBigInteger

所以有两种方法可以解决只选择一种

  1. 编辑要bigincrementsposts迁移中的id
  2. 在"媒体"迁移到unsignedIntegere中编辑您的posts_id

最新更新