Foreign key constraint is incorrectly formed with Composite Keys

烂漫一生 提交于 2019-12-11 17:22:48

问题


I have a table "theaters". In which (theater_name,area_name,station) are composite key.And in the table "cubelists" I have columns (thtr_name,area,stn) which are to be referred to (theater_name,area_name,station) of table "theaters".

The problem here is the combination of the columns - (theater_name,area_name,station) in table "theaters" is unique as it is a composite key. But each column separately is not unique.

Then how can I refer these columns from table "cubelists"?

Schema::create('theaters', function (Blueprint $table) {

        $table->string('theater_name');
        $table->string('area_name');
        $table->string('station');
        $table->primary(array('theater_name','area_name','station'));
        $table->text('address');
        $table->bigInteger('phno');
        $table->string('contact_person');

    });



  public function up()
    {
    //
    Schema::create('cubelists', function (Blueprint $table) {
        $table->string('mvie_name');
        $table->foreign('mvie_name')->references('movie_name')->on('movies');
        $table->string('thtr_name');

        $table->string('area');

        $table->string('stn');

        $table->foreign(array('thtr_name','area','stn'))-
        >references(array('theater_name','area_name','station'))-
        >on('theaters');
        $table->primary(array('mvie_name','thtr_name','area','stn'));
        $table->string('type');
        $table->string('subtype');
        $table->date('validity');
        $table->string('show');

    });

}

If I give the above code I get an error as

Migration table created successfully.


  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `boras_cachii`.`#sql-a10_
  112` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tabl
  e `agreements` add constraint agreements_area_name_foreign foreign key (`area_nam
  e`) references `cubelists` (`area`))



  [PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `boras_cachii`.`#sql-a10_
  112` (errno: 150 "Foreign key constraint is incorrectly formed")

回答1:


You must first create the table, then foreign keys.

Schema::create('cubelists', function (Blueprint $table) {
        $table->string('mvie_name');
        $table->string('area');
        $table->string('stn');
        $table->primary(array('mvie_name','thtr_name','area','stn'));
        $table->string('type');
        $table->string('subtype');
        $table->date('validity');
        $table->string('show');

           $table->foreign(array('thtr_name','area','stn'))
            ->references(array('theater_name','area_name','station'))
             ->on('theaters');

           $table->foreign('mvie_name')
           ->references('movie_name')
           ->on('movies');


    });

Also theaters table must migrate first since cubelists is referencing it. And make sure that the foreign key column and the referencing column are the same type or length.



来源:https://stackoverflow.com/questions/45664138/foreign-key-constraint-is-incorrectly-formed-with-composite-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!