Back

General error: 1215 Cannot add foreign key constraint


Here are my migrations: photoset_categories

class CreatePhotosetCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('photoset_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->comment('Название категории');
        });
    }
}

models

class CreateModelsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('models', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsingned()->comment('ID модели');

            /**
             * Основная часть
             */
            $table->string('birth')->default(NULL)->nullable()->comment('Дата рождения');
            $table->string('d_birth')->default('')->comment('День рождения');
            $table->string('m_birth')->default('')->comment('Месяц рождения');
            $table->string('y_birth')->default('')->comment('Год рождения');

            $table->timestamps();
        });
    }
}

And model_photosets

class CreateModelPhotosetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('model_photosets', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('model_id')->unsigned()->index()->comment('ID модели');
            $table->foreign('model_id')->references('user_id')->on('models')->onDelete('cascade');

            $table->integer('category_id')->unsigned()->index()->comment('ID категории');
            $table->foreign('category_id')->references('id')->on('photoset_categories')->onDelete('cascade');
        });
    }
}

Error:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter
  table `model_photosets` add constraint `model_photosets_model_id_foreign` foreign k
  ey (`model_id`) references `models` (`user_id`) on delete cascade)
rowright replied 8 months ago

Generally that means a type error between the two columns. I think there is a problem is the "models" table as the user_id on it might not be unsigned because there's a spelling error on the "->unsigned()" which would prevent it from being an unsigned int like on the "model_photosets" table. Check your database to confirm if you have the right data type on the models table.

MyZik replied 8 months ago

rowright said:

Thanks for the answer. I tried to correct the spelling error in the type name (table models) and completely remove it, but it did not help me :c

saurabhd replied 8 months ago

@MyZik:

Follow below steps:

  1. Run migration first for 'photoset_categories' and 'models'
  2. Run migration for 'model_photosets' once above step 1) migration done

Here, the problem is your 'model_photosets' table migration run before the foreign key table migration(i.e. 'photoset_categories' and 'models')

Hope this works for you!

MyZik replied 8 months ago

saurabhd said:

Hi! Thanks for the answer. I did everything as you said, but the error remained. I noticed a feature: when I change foreign key user_id to id it works. But this is not what I need.

saurabhd replied 7 months ago

Use below code in your CreateModelPhotosetsTable migration file

class CreateModelPhotosetsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('model_photosets', function (Blueprint $table) {
           $table->increments('id');
           $table->integer('model_id')->unsigned();
           $table->foreign('model_id')->references('user_id')->on('models')->onDelete('cascade')->unique()->unsigned();

           $table->integer('category_id')->unsigned();
           $table->foreign('category_id')->references('id')->on('photoset_categories')->onDelete('cascade');
       });
   }
}

Hope this work for you !


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us