【发布时间】:2021-08-06 03:12:31
【问题描述】:
为什么使用 laravel 模式回复:
SQLSTATE[HY000]:一般错误:1005 无法创建表
employee_management.employees(errno: 150 "外键约束 格式不正确”)(SQL:alter tableemployees添加约束employees_city_id_foreign外键 (city_id) 引用city(id))PDOException::("SQLSTATE[HY000]: 一般错误:1005 无法创建 表
employee_management.employees(errno: 150 "外键 约束格式不正确")")
我的桌子:
Schema::create('employees', function (Blueprint $table) {
$table->increments('id', true);
$table->string('lastname', 60);
$table->string('firstname', 60);
$table->string('middlename', 60);
$table->string('address', 120);
$table->integer('city_id')->unsigned();
$table->integer('state_id')->unsigned();
$table->integer('country_id')->unsigned();;
$table->foreign('city_id')->references('id')->on('city');
$table->foreign('state_id')->references('id')->on('state');
$table->foreign('country_id')->references('id')->on('country');
$table->char('zip', 10);
$table->integer('age')->unsigned();
$table->date('birthdate');
$table->date('date_hired');
$table->integer('department_id')->unsigned();
$table->integer('division_id')->unsigned();
// $table->integer('company_id')->unsigned();
$table->foreign('department_id')->references('id')->on('department');
$table->foreign('division_id')->references('id')->on('division');
// $table->foreign('company_id')->references('id')->on('company');
$table->string('picture', 60);
$table->timestamps();
$table->softDeletes();
});
我的第二张城市表:
Schema::create('city', function (Blueprint $table) {
$table->increments('id', true);
$table->integer('state_id')->unsigned();
$table->foreign('state_id')->references('id')->on('state');
$table->string('name', 60);
$table->timestamps();
$table->softDeletes();
});
附:我的 Laravel 版本是 8.12
【问题讨论】: