SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint - Laravel

前端 未结 1 584
忘了有多久
忘了有多久 2021-01-24 08:50

I\'m trying to create a table to hold photos and link it to the ad (property id). I\'m getting this error

SQLSTATE[HY000]: General error: 1215 Cannot add

相关标签:
1条回答
  • 2021-01-24 09:38

    Make it unsigned because you're using increments(). And move FK constraint part to a separate closure:

    public function up()
    {
        Schema::create('property_advert_photos', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('propertyadvert_id')->nullable();
            $table->timestamps();
        });
    
        Schema::table('property_advert_photos', function (Blueprint $table) {
            $table->foreign('propertyadvert_id')->references('id')->on('property_adverts');
        });
    }
    
    0 讨论(0)
提交回复
热议问题