php - 错误号 : 150 "Foreign key constraint is incorrectly formed" how can i fix this?

标签 php mysql laravel migration

我一直在以不同的方式尝试它,但我无法消除错误。所以我的问题是:其他人能看出错误吗?

这是我的代码:

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_idcreated_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/

相关文章:

javascript - 如果需要,显示字段

PHP - 通过复选框删除多行

MySQL 按 ID 和最新日期时间分组

laravel - 将 Laravel 从 vagrant 连接到主机 MSSQL express

iis - 使用 Laravel Homestead 将 Vagrant 端口转发 80 到 8000

php - 处理图像的现成解决方案

php - 使用 PHP 变量自动填充文本区域

mysql - 将 CSV 文件加载到 MySQL Workbench

php - MySQL UTF-8 字符插入问题

laravel - 检查 to_date 之前的 from_date 和 from_date 之后的 to_date,也在 laravel nova 验证中检查今天之前的两个日期