外键约束格式不正确 - 拉拉维尔



我有两个表"剧院"和"立方体列表"。 "剧院"表已经创建。 在"立方体列表"表中,我将"area"和"stn"作为表"剧院"的外键。 但是我收到一个错误,外键约束格式不正确。 但是我无法弄清楚错误。

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:xampphtdocsboras>php artisan migrate
Migration table created successfully.

[IlluminateDatabaseQueryException]
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")

外键约束应引用候选键。我不知道立方体列表是什么意思,但你可能想引用"剧院"中的主键。也就是说,您可能希望一个外键约束引用三列theater_name、area_name和工作站。不是对每一列的单独约束。

MySQL可能会也可能不会让你逃脱你最初试图做的事情,但无论如何都不要这样做。搜索 13.1.17.6 使用外键约束"不强制执行"。

最新更新