php - Laravel 6-MariaDB 10.1 : Illuminate\Database\QueryException : SQLSTATE[HY000] migration error

标签 php mysql laravel mariadb

我有自定义迁移:

代码:

// Groups migration
Schema::create('groups', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->boolean('status')->default(false);
    $table->timestamps();
});

// Clients migration
Schema::create('clients', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('fullname');
    $table->integer('phone');
    $table->date('birthday')->nullable();
    $table->boolean('can_get_congratulations')->default(false);
    $table->unsignedInteger('group_id')->default(null);
    $table->foreign('group_id')
          ->references('id')
          ->on('groups')
          ->onDelete('cascade');
    $table->boolean('status')->default(true);
    $table->timestamps();
});

当我运行此迁移文件时收到错误消息:

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table taxisms.#sql-1cc0_65c (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table clients add constraint clients_group_id_foreign foreign key (group_id) references groups (id) on delete cascade)

enter image description here

我的迁移代码哪里有错误?

最佳答案

该列两侧都需要匹配。由于groups.id是一个无符号大整数,group_id也需要如此。改变

$table->unsignedInteger('group_id') 

$table->unsignedBigInteger('group_id')

关于php - Laravel 6-MariaDB 10.1 : Illuminate\Database\QueryException : SQLSTATE[HY000] migration error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59076820/

相关文章:

javascript - 如何更改使用jquery中的循环创建的文本框onchange的值

php - 使用 PHP 显示来自 MySQL 数据库的希腊文本的问题

php - Laravel 多站点

php - laravel - 给定可变用户输入生成组合

php - Laravel 资源 Controller ajax 调用

php - 如何处理特定身份验证守卫的登录事件?

php - 使用变量更新数据库表

php - mysql_real_escape_string() : Access denied in codeigniter

Mysql - 获取上周带有时间戳的条目数占总数的百分比

PHP->MySQL 表单 - 验证