0

J'ai une table "théâtres". Dans lequel (theater_name, area_name, station) sont des clés composites. Et dans la table "cubelists" j'ai des colonnes (thtr_name, area, stn) auxquelles il faut se référer (theater_name, area_name, station) de la table "theaters".La contrainte de clé étrangère est incorrectement formée avec des touches composites

Le problème ici est la combinaison des colonnes - (nom_théâtre, nom_zone, station) dans la table "théâtres" est unique car il s'agit d'une clé composite. Mais chaque colonne séparément n'est pas unique.

Alors comment puis-je référencer ces colonnes de la 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'); 

    }); 

} 

Si je donne le code ci-dessus je reçois une erreur

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") 
+0

boras_cachii est le nom de la base de données – Muthu

Répondre

1

Vous devez d'abord créer la table, puis les clés étrangères.

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'); 


    }); 

également la table theaters doit migrer d'abord depuis cubelists est son référencement. Et assurez-vous que la colonne de clé étrangère et la colonne de référence sont du même type ou de la même longueur.

+0

J'ai créé une table de théâtres et l'ai migrée. Encore je reçois le même Erreur – Muthu

+0

aller à config/database.php sur mysql ensemble strict à faux –

+0

Merci !! Ça a marché!! – Muthu