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")
根據您的數據庫引擎,語法可能需要在添加約束[名稱]外鍵([列])引用[表]([othercolumn])上刪除[做某事]上更新[做某事] ,但「刪除/更新」部分似乎缺失。我想你需要添加' - > onDelete('cascade') - > onUpdate('cascade')'給你的電話,以確保它被添加。 –
@NiettheDarkAbsol我認爲OP應該使用MariaDB,因爲他使用XAMPP – Thamilan
我嘗試添加 - > onDelete('cascade') - > onUpdate('cascade'),但這不起作用 – Muthu