我试图在Laravel中创建外键,但是当我使用artisan迁移我的表时,我抛出了以下错误:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `priorities` add constraint priorities_user_id_foreign foreign 
key (`user_id`) references `users` (`id`))     

我的迁移代码如下:

优先级迁移文件

public function up()
{
    //
    Schema::create('priorities', function($table) {
        $table->increments('id', true);
        $table->integer('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('priority_name');
        $table->smallInteger('rank');
        $table->text('class');
        $table->timestamps('timecreated');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
    Schema::drop('priorities');
}

用户迁移文件

public function up()
{
    //
    Schema::table('users', function($table)
    {
    $table->create();
    $table->increments('id');
    $table->string('email');
    $table->string('first_name');
    $table->string('password');
    $table->string('email_code');
    $table->string('time_created');
    $table->string('ip');
    $table->string('confirmed');
    $table->string('user_role');
    $table->string('salt');
    $table->string('last_login');

    $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
        Schemea::drop('users');
}

关于我做错了什么,我现在想要得到这个,因为我有很多表,我需要创建例如用户,客户端,项目,任务,状态,优先级,类型,团队。理想情况下,我想创建表持有这些数据与外键,即clients_project和project_tasks等。

希望有人能帮我开始。


当前回答

在我的例子中,问题是用户表的自动生成迁移正在设置中

...
$table->bigIncrements('id');
...

所以我必须改变列的类型


$table->bigInteger('id');

使我的迁移与外键工作。

这是laravel 5.8.2

其他回答

注意:当Laravel设置一个表使用

$table->increments('id');

这在大多数迁移中是标准的,这将设置一个无符号整数字段。因此,当从另一个表对这个字段进行外部引用时,确保在引用表中,您将字段设置为UnsignedBigInteger,而不是(我假设是一个)UnsignedBigInteger字段。

例如:在迁移文件2018_12_12_123456_create_users_table.php中:

Schema::create('users', function (Blueprint $table){
    $table->increments('id');
    $table->string('name');
    $table->timestamps();

然后在迁移文件2018_12_12_18000000_create_permissions_table.php中,该文件将外部引用设置回用户:

Schema::create('permissions', function (Blueprint $table){
    $table->increments('id');
    $table->UnsignedInteger('user_id'); // UnsignedInteger = "increments" in users table
    $table->boolean('admin');
    $table->boolean('enabled');
    $table->timestamps();

    // set up relationship
    $table->foreign('user_id')->reference('id')->on('users')->onDelete('cascade');
}

其要点是,外部方法使用ALTER_TABLE将一个预先存在的字段转换为外键。所以在应用外键之前必须定义表类型。但是,它不必在单独的Schema::调用中。你可以在create中做这两件事,像这样:

public function up()
{
    Schema::create('priorities', function($table) {
        $table->increments('id', true);
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('priority_name');
        $table->smallInteger('rank');
        $table->text('class');
        $table->timestamps('timecreated');
    });
}

还要注意,user_id的类型被设置为unsigned以匹配外键。

在我的例子中,问题是用户表的自动生成迁移正在设置中

...
$table->bigIncrements('id');
...

所以我必须改变列的类型


$table->bigInteger('id');

使我的迁移与外键工作。

这是laravel 5.8.2

如果以上解决方案都不适合新手,检查两个id是否具有相同的类型:都是整数或都是bigInteger,…你可以有这样的东西:

主表(例如用户)

$table->bigIncrements('id');

子表(例如优先级)

$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

此查询将失败,因为用户。id是一个大整数,而优先级。user_id是一个整数。

在这种情况下,正确的查询如下:

$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

请先检查列的数据类型。如果第一个表的id类型是bigint,那么当添加外键时,检查其他表的类型应该是bigint。