MySQL Error 1215: Cannot add foreign key constraint

前端 未结 30 2563
予麋鹿
予麋鹿 2020-11-22 01:37

I am trying to forward engineer my new schema onto my db server, but I can\'t figure out why I am getting this error. I\'ve tried to search for the answer here, but everyth

相关标签:
30条回答
  • 2020-11-22 02:06

    Wooo I just got it ! It was a mix of a lot of already posted answers (innoDB, unsigned, etc). One thing I didn't see here though is : if your FK is pointing on a PK, ensure the source column has a value that makes sense. For example, if the PK is a mediumint(8), make sure the source column also contains a mediumint(8). That was part of the problem for me.

    0 讨论(0)
  • 2020-11-22 02:06

    when try to make foreign key when using laravel migration

    like this example:

    user table

        public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->TinyInteger('color_id')->unsigned();
            $table->foreign('color_id')->references('id')->on('colors');
            $table->timestamps();
        });
    }
    

    colors table

        public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('color');
            $table->timestamps();
        });
    }
    

    sometimes properties didn't work

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

    this error happened because the foreign key (type) in [user table] is deferent from primary key (type) in [colors table]

    To solve this problem should change the primary key in [colors table]

    $table->tinyIncrements('id');


    When you use primary key $table->Increments('id');

    you should use Integer as a foreign key

        $table-> unsignedInteger('fk_id');
        $table->foreign('fk_id')->references('id')->on('table_name');
    

    When you use primary key $table->tinyIncrements('id');

    you should use unsignedTinyInteger as a foreign key

        $table-> unsignedTinyInteger('fk_id');
        $table->foreign('fk_id')->references('id')->on('table_name');
    

    When you use primary key $table->smallIncrements('id');

    you should use unsignedSmallInteger as a foreign key

        $table-> unsignedSmallInteger('fk_id');
        $table->foreign('fk_id')->references('id')->on('table_name');
    

    When you use primary key $table->mediumIncrements('id');

    you should use unsignedMediumInteger as a foreign key

        $table-> unsignedMediumInteger('fk_id');
        $table->foreign('fk_id')->references('id')->on('table_name');
    
    0 讨论(0)
  • 2020-11-22 02:07

    Even i had the same problem . And the fault was with the "unsigned" marker in the FK's table PK

    0 讨论(0)
  • 2020-11-22 02:09

    When this error occurrs because the referenced table uses the MyISAM engine this answer provides a quick way to convert your database so all Django model tables use InnoDB: https://stackoverflow.com/a/15389961/2950621

    It's a Django management command called convert_to_innodb.

    0 讨论(0)
  • 2020-11-22 02:10

    I got the same error while trying to add an fk. In my case the problem was caused by the FK table's PK which was marked as unsigned.

    0 讨论(0)
  • 2020-11-22 02:10

    I experienced this error for a completely different reason. I used MySQL Workbench 6.3 for creating my Data Model (awesome tool). I noticed that when the column order defined in the Foreign Key constraint definition does not fit the table column sequence this error is also generated.

    It took me about 4 hours of trying everything else but checking that.

    Now all is working well and I can get back to coding. :-)

    0 讨论(0)
提交回复
热议问题