我有一個表「劇院」。其中(theater_name,area_name,station)是組合鍵。在表格「立方體列表」中,我有列表(thtr_name,area,stn),它們將被引用(劇院名稱,area_name,電臺)爲表格「劇院」。用複合鍵錯誤地形成外鍵約束
這裏的問題是表中「劇院」中的列(劇院名,區域名,電臺)的組合是唯一的,因爲它是一個組合鍵。但是,每一列分別不是唯一的。
那麼我該如何從表格「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');
});
}
如果我給上面的代碼中,我得到一個錯誤的
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")
boras_cachii是數據庫的名稱 – Muthu