我有几对表Client,Rooms和Reservations。当我尝试迁移时,我遇到了这种错误:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table
`reservations` add constraint `reservations_client_id_foreign` foreign key (`client_id`) references `clients` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed")
这是我的预订迁移代码:
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->increments('id');
$table->date('date_in');
$table->date('date_out');
$table->integer('client_id')->unsingned;
$table->foreign('client_id')->references('id')->on('clients');
$table->integer('room_id')->unsingned;
$table->foreign('room_id')->references('id')->on('rooms');
$table->timestamps();
});
}
有些人说这是因为我的迁移顺序,但我认为它已经是正确的顺序,这里是它的截图:
那么如何才能在没有任何错误的情况下进行迁移呢?感谢。
答案 0 :(得分:0)
您的拼写错误.. unsigned
而非unsingned
而您忘记使用()
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->increments('id');
$table->date('date_in');
$table->date('date_out');
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients');
$table->integer('room_id')->unsigned();
$table->foreign('room_id')->references('id')->on('rooms');
$table->timestamps();
});
}