2017-08-13 86 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为“不执行”。