我一直在以不同的方式尝试它,但我无法消除错误。所以我的问题是:其他人能看出错误吗?
这是我的代码:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id')->unique();
$table->boolean('admin')->default('0');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function up()
{
Schema::create('places', function (Blueprint $table) {
$table->bigIncrements('id')->unique();
$table->string('location_name');
$table->string('village');
$table->string('street');
$table->integer('number')->unsigned();
});
}
public function up()
{
Schema::create('planning', function (Blueprint $table) {
$table->bigIncrements('id')->unique();
$table->unsignedInteger('places_id');
$table->time('van');
$table->time('tot');
$table->date('dag');
$table->timestamps();
$table->unsignedInteger('created_by');
$table->foreign('places_id')
->references('id')
->on('places')
->onDelete('cascade');
$table->foreign('created_by')
->references('id')
->on('users')
->onDelete('cascade');
});
}
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `foodtruck`.`#sql-3be8_b8` (errno: 150 "Foreign key constraint is incorrectly formed" )")
我希望此错误消息保留我的命令行和我的迁移文件,以我可以正常使用的方式进行修复:)
最佳答案
因为 places_id
和 created_by
被定义为 bigIncrements
,你不能将它们的外键定义为它需要的 unsignedInteger
是根据documentation对应的数据类型是:
Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column.
相当于 unsignedBigInteger
。
改变,
$table->unsignedInteger('places_id');
$table->unsignedInteger('created_by');
为了,
$table->unsignedBigInteger('places_id');
$table->unsignedBigInteger('created_by');
关于php - 错误号 : 150 "Foreign key constraint is incorrectly formed" how can i fix this?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57909706/