我试图在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等。

希望有人能帮我开始。


当前回答

在我的例子中,我引用了字符串user_id列上的整数id列。我改变了:

表- >字符串(“user_id”)

to:

表- >美元整数(user_id) - >无符号();

希望它能帮助到一些人!

其他回答

你可以直接在整型列中传递布尔参数,说它是否应该是无符号的。在laravel 5.4下面的代码解决了我的问题。

        $table->integer('user_id', false, true);

这里第二个参数false表示它不应该是自动递增的,第三个参数true表示它应该是无符号的。可以将外键约束保留在同一迁移中,也可以将其分离。两者都适用。

你应该这样写

public function up()
{
    Schema::create('transactions', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->float('amount', 11, 2);
        $table->enum('transaction type', ['debit', 'credit']);
        $table->bigInteger('customer_id')->unsigned();      
        $table->timestamps();                 
    });

    Schema::table('transactions', function($table) {
        $table->foreign('customer_id')
              ->references('id')->on('customers')
              ->onDelete('cascade');
    });     
}

外键字段应不签名,希望对您有帮助!!

拉拉维尔 ^5.8

从Laravel 5.8开始,迁移存根在ID上使用bigIncrements方法 列。方法创建ID列 增量的方法。 这不会影响项目中的任何现有代码;然而,是 注意外键列必须具有相同的类型。因此,一个 使用increments方法创建的列不能引用列 使用bigIncrements方法创建。 来源:Migrations & bigIncrements


例子

假设您正在构建一个简单的基于角色的应用程序,并且需要在PIVOT表“role_user”中引用user_id。

2019年_05_05_112458_create_users_table.php

// ... public function up() { Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('full_name'); $table->string('email'); $table->timestamps(); }); } 2019_05_05_120634_create_role_user_pivot_table.php // ... public function up() { Schema::create('role_user', function (Blueprint $table) { // this line throw QueryException "SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint..." // $table->integer('user_id')->unsigned()->index(); $table->bigInteger('user_id')->unsigned()->index(); // this is working $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); }); } As you can see, the commented line will throw a query exception, because, as mentioned in the upgrade notes, foreign key columns must be of the same type, therefore you need to either change the foreing key (in this example it's user_id) to bigInteger in role_user table or change bigIncrements method to increments method in users table and use the commented line in the pivot table, it's up to you. I hope i was able to clarify this issue to you.

(学习英语,不好意思) 我尝试在我的项目与“foreignId”和工作。在你的代码中只是删除列user_id,并在引用上添加foreignId:

 public function up()
{

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

IMPORTANTE:在本例中,首先创建没有外键的表,即“users”表

上面所有的解决方案都是正确的,但没有一个适合我。

为我创造问题的东西是数据库表引擎。引擎类型已经添加的表是MyISAM和新的迁移正在设置引擎类型= 'InnoDB'

我只是设置引擎类型已经添加表到InnoDB,就是这样!