2017-08-13 107 views
0

我有兩個表格「劇院」和「立方體表」。 「劇院」表已經創建好了。在「立方體表」中,我將「area」和「stn」作爲表格「劇院」的外鍵。但是,我得到一個錯誤,外鍵約束錯誤地形成。但我無法弄清楚這個錯誤。外鍵約束錯誤地形成 - Laravel

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

}); 

Schema::create('cubelists', function (Blueprint $table) { 
    $table->string('mvie_name'); 
    $table->foreign('mvie_name')->references('movie_name')->on('movies'); 
    $table->string('thtr_name'); 
    $table->foreign('thtr_name')->references('theater_name')- 
    >on('theaters'); 
    $table->string('area'); 
    $table->foreign('area')->references('area_name')->on('theaters'); 
    $table->string('stn'); 
    $table->foreign('stn')->references('station')->on('theaters'); 
    $table->primary(array('mvie_name','thtr_name','area','stn')); 
    $table->string('type'); 
    $table->string('subtype'); 
    $table->date('validity'); 
    $table->string('show'); 

}); 

錯誤是

C:\xampp\htdocs\boras>php artisan migrate 
Migration table created successfully. 


    [Illuminate\Database\QueryException] 
    SQLSTATE[HY000]: General error: 1005 Can't create table `boras_cachii`.`#sql-a10_ 
    5a` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table 
    `cubelists` add constraint cubelists_area_foreign foreign key (`area`) reference 
    s `theaters` (`area_name`)) 



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

根據您的數據庫引擎,語法可能需要在添加約束[名稱]外鍵([列])引用[表]([othercolumn])上刪除[做某事]上更新[做某事] ,但「刪除/更新」部分似乎缺失。我想你需要添加' - > onDelete('cascade') - > onUpdate('cascade')'給你的電話,以確保它被添加。 –

+0

@NiettheDarkAbsol我認爲OP應該使用MariaDB,因爲他使用XAMPP – Thamilan

+0

我嘗試添加 - > onDelete('cascade') - > onUpdate('cascade'),但這不起作用 – Muthu

回答

0

外鍵約束應該引用的候選鍵。我不知道是什麼意思,意思是,但你可能想引用「劇院」中的主鍵。也就是說,你可能是想要一個外鍵約束來引用三個列的theater_name,area_name和station。不是每個這些列的個別約束。

MySQL可能會也可能不會讓你逃脫你最初嘗試做的事情,但不要那樣做。搜索13.1.17.6 Using FOREIGN KEY Constraints爲「不執行」。