繁体   English   中英

迁移:无法添加外键约束

[英]Migration: Cannot add foreign key constraint

我正在尝试在 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_projectproject_tasks等。

希望有人可以帮助我开始。

分两步添加,也可以不签名:

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

   Schema::table('priorities', function($table) {
       $table->foreign('user_id')->references('id')->on('users');
   });

}

问题已经回答,但希望这可能对其他人有所帮助。

这个错误发生在我身上,因为我首先创建了带有外键的迁移表,然后该键作为其原始表中的主键存在。 迁移按照它们创建的顺序执行,如运行migrate:make后生成的文件名所示。 例如2014_05_10_165709_create_student_table.php

解决方案是将带有外键的文件重命名为比带有主键的文件更早的时间,如此处推荐的那样: http : //forumsarchive.laravel.io/viewtopic.php?id=10246

我想我还必须添加$table->engine = 'InnoDB';

Laravel ^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');
    });
}

如您所见,注释行将抛出查询异常,因为如升级说明中所述,外键列必须为相同类型,因此您需要将前键(在本例中为user_id )更改为BIGINTEGER用户ROLE_USER表或改变bigIncrements方法增量法和使用数据透视表中注释行,就看你了。


我希望我能够向你澄清这个问题。

就我而言,问题是主表中已经有记录,我强制新列不为 NULL。 因此,向新列添加 ->nullable() 就成功了。 在问题的例子中会是这样的:

$table->integer('user_id')->unsigned()->nullable();

或者:

$table->unsignedInteger('user_id')->nullable();

希望这对某人有帮助!

在我的情况下,问题是users表的自动生成的迁移正在设置

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

所以我不得不改变列类型


$table->bigInteger('id');

使我的外键迁移工作。

这与 Laravel 5.8.2

在我的情况下,问题出在迁移时间上,在创建迁移时要小心,首先创建子迁移而不是基本迁移。 因为如果您首先创建具有外键的基本迁移,则会查找子表,并且不会有随后抛出异常的表。

此外:

创建迁移时,它的开头有一个时间戳。 假设你已经创建了一个迁移猫,所以它看起来像2015_08_19_075954_the_cats_time.php并且它有这个代码

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class TheCatsTime extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cat', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');  
            $table->date('date_of_birth');
            $table->integer('breed_id')->unsigned()->nullable(); 
        });

        Schema::table('cat', function($table) {
        $table->foreign('breed_id')->references('id')->on('breed');
      });
    }

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

创建基表后,您创建另一个迁移品种,即子表,它有自己的创建时间和日期戳。 代码将如下所示:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class BreedTime extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('breed', function (Blueprint $table) {
             $table->increments('id');    
             $table->string('name');
        });
    }

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

看起来这两个表都是正确的,但是当您运行php artisan migrate 时 它将抛出异常,因为迁移将首先在您的数据库中创建基表,因为您首先创建了此迁移,并且我们的基表中具有外键约束,它将查找子表并且子表不存在,这可能是一个例外..

所以:

首先创建子表迁移。

创建子迁移后创建基表迁移。

php工匠迁移。

完成它会起作用

在 laravel 5.8 中, users_table 使用bigIncrements('id')数据类型作为主键。 因此,当您想引用外键约束时,您的user_id列需要是unsignedBigInteger('user_id')类型。

我在 Laravel 5.8 上遇到了这个问题,我修复了这段代码,如Laravel 文档中所示,我在添加外键的地方。

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

然后我运行$ php artisan migrate:refresh

由于这种语法相当冗长,Laravel 提供了额外的、更简洁的方法,这些方法使用约定来提供更好的开发人员体验。 上面的例子可以这样写:

Schema::table('posts', function (Blueprint $table) {
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
});

就我而言,我只是更改手动执行迁移的顺序,因此首先创建表用户。

在文件夹 database/migrations/ 中,您的迁移文件名具有以下格式:year_month_day_hhmmss_create_XXXX_table.php

只需重命名创建用户文件,以便您的表优先级表的创建日期设置为晚于用户日期(即使晚一秒就足够了)

我在使用Laravel 5.8时遇到了同样的问题 在仔细查看 laravel 文档之后,此外还有迁移和 bigIncrements 我解决它的方法是将主键"$table->bigIncrements('id')" 添加到与表"users"及其关联相关的每个表中,在我的例子中是表"role" 最后,我有"$table->unsignedBigInteger"用于将角色关联到用户(多对多),即表"role_user"

1. Users table

    Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

2. Roles Table
    Schema::create('roles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name')->unique();
        $table->string('display_name')->nullable();
        $table->string('description')->nullable();
        $table->timestamps();
    });

3. Table role_user
Schema::create('role_user', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('role_id');
            $table->foreign('user_id')->references('id')->on('users')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('roles')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->primary(['user_id', 'role_id']);
        });

这个错误发生在我身上,因为 - 虽然我试图创建的表是 InnoDB - 我试图将它关联到的外部表是 MyISAM 表!

使用 Laravel 5.3 也有同样的问题。

解决方案是使用unsignedInteger而不是integer('name')->unsigned()

所以这就是有效的

$table->unsignedInt('column_name');
$table->foreign('column_name')->references('id')->on('table_name');

这样做的原因是当使用integer('name')->unsigned时,表中创建的列的长度为 11,但使用unsigedInteger('name') 时,该列的长度为 10。

长度 10 是使用 Laravel 时主键的长度,因此列长度匹配。

我们不能添加关系,除非相关表被创建。 Laravel 按迁移文件的日期运行迁移顺序。 因此,如果您想与第二个迁移文件中存在的表创建关系,它将失败。

我遇到了同样的问题,所以我最后又创建了一个迁移文件来指定所有关系。

Schema::table('properties', function(Blueprint $table) {
        $table->foreign('user')->references('id')->on('users')->onDelete('cascade');
        $table->foreign('area')->references('id')->on('areas')->onDelete('cascade');
        $table->foreign('city')->references('id')->on('cities')->onDelete('cascade');
        $table->foreign('type')->references('id')->on('property_types')->onDelete('cascade');
    });

    Schema::table('areas', function(Blueprint $table) {
        $table->foreign('city_id')->references('id')->on('cities')->onDelete('cascade');
    });

你应该这样写

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');
    });     
}

外键字段应该是unsigned ,希望它有帮助!!

请注意:当 Laravel 使用

$table->increments('id');

这是大多数迁移的标准,这将设置一个无符号整数字段。 因此,当从另一个表对该字段进行外部引用时,请确保在引用表中将该字段设置为 UnsignedInteger 而不是(我认为是)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');
}

为了在 Laravel 中添加外键约束,以下对我有用:

  1. 创建列作为外键,如下所示:

      $table->integer('column_name')->unsigned();
  2. 在(1)之后立即添加约束线即

     $table->integer('column_name')->unsigned();\n $table->foreign('column_name')->references('pk_of_other_table')->on('other_table');

我知道那是一个老问题,但请确保您使用的参考文献是否定义了正确的支持引擎。 为两个表设置 innodb 引擎,并为引用列设置相同的数据类型

$table->engine = 'InnoDB';

我注意到的一件事是,如果表使用不同的引擎而不是外键约束不起作用。

例如,如果一张表使用:

$table->engine = 'InnoDB';

而其他用途

$table->engine = 'MyISAM';

会产生一个错误:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

您可以通过在表创建结束时添加 InnoDB 来解决此问题,如下所示:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('business_unit_id')->nullable();

        $table->string('name', 100);

        $table->foreign('business_unit_id')
                ->references('id')
                ->on('business_units')
                ->onDelete('cascade');

        $table->timestamps();
        $table->softDeletes();
        $table->engine = 'InnoDB'; # <=== see this line
    });
}

在最初的问题几年后,使用 laravel 5.1,我遇到了同样的错误,因为我的迁移是用所有相同的日期代码由计算机生成的。 我浏览了所有建议的解决方案,然后重构以找到错误源。

在关注 laracasts 和阅读这些帖子时,我相信正确的答案类似于 Vickies 的答案,除了您不需要添加单独的架构调用。 您不需要将表设置为 Innodb,我假设 laravel 现在正在这样做。

迁移只需要正确计时,这意味着您将在文件名中修改(稍后)需要外键的表的日期代码。 或者或另外,降低不需要外键的表的日期代码。

修改日期代码的优点是您的迁移代码更易于阅读和维护。

到目前为止,我的代码正在通过调整时间码来推迟需要外键的迁移。

但是我确实有数百张表,所以最后我只有一张外键表。 只是为了让事情顺利进行。 我假设我会将它们拉入正确的文件并在我测试它们时修改日期代码。

举个例子:文件 2016_01_18_999999_create_product_options_table。 这个需要创建 products 表。 查看文件名。

 public function up()
{
    Schema::create('product_options', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('product_attribute_id')->unsigned()->index();
        $table->integer('product_id')->unsigned()->index();
        $table->string('value', 40)->default('');
        $table->timestamps();
        //$table->foreign('product_id')->references('id')->on('products');
        $table->foreign('product_attribute_id')->references('id')->on('product_attributes');
        $table->foreign('product_id')->references('id')->on('products');


    });
}

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

产品表:这需要先迁移。 2015_01_18_000000_create_products_table

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');

        $table->string('style_number', 64)->default('');
        $table->string('title')->default('');
        $table->text('overview')->nullable();
        $table->text('description')->nullable();


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

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

最后是我临时用来解决问题的文件,当我为我命名为 9999_99_99_999999_create_foreign_keys.php 的模型编写测试时,我将重构该文件。 这些键在我拔出时已被注释,但您明白了。

    public function up()
    {
//        Schema::table('product_skus', function ($table) {
//            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
//    });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
//        Schema::table('product_skus', function ($table)
//        {
//            $table->dropForeign('product_skus_product_id_foreign');
//        });

很简单 !!!

如果您第一次创建'priorities'迁移文件,Laravel 首先运行'priorities''users'表不存在。

它如何添加与不存在的表的关系!。

解决方案:从'priorities'表中提取外键代码 你的迁移文件应该是这样的:

在此处输入图片说明

并添加到一个新的迁移文件,这里它的名称是create_prioritiesForeignKey_table并添加以下代码:

public function up()
{        
    Schema::table('priorities', function (Blueprint $table) {          
        $table->foreign('user_id')
              ->references('id')
              ->on('users');                        
    });
}

确保您的前列超过前键列的范围

我的意思是你的外键(在第二个表中)必须与你的 ponter 主键(在第一个表中)相同类型

您的指针主键必须是添加无符号方法,让我展示一下:

在您的第一个迁移表上:

$table->increments('column_name'); //is INTEGER and UNSIGNED

在您的第二个迁移表上:

$table->integer('column_forein_name')->unsigned(); //this must be INTEGER and UNSIGNED
$table->foreign('column_forein_name')->references('column_name')->on('first_table_name');

另一个看到差异的例子

在您的第一个迁移表上:

$table->mediumIncrements('column_name'); //is MEDIUM-INTEGER and UNSIGNED

在您的第二个迁移表上:

$table->mediumInteger('column_forein_name')->unsigned(); //this must be MEDIUM-INTEGER and UNSIGNED
$table->foreign('column_forein_name')->references('column_name')->on('first_table_name');

查看 MYSQL 数字类型表范围

如果上述解决方案都不适用于新手,请检查两个 ID 是否具有相同的类型:都是integer或都是bigInteger ,......你可以有这样的东西:

主表(例如用户)

$table->bigIncrements('id');

子表(例如优先级)

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

此查询将失败,因为users.id是一个BIG INTEGERpriorities.user_id是一个INTEGER

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

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

也可能是你创建迁移的顺序。 如果你先创建优先级表,然后创建用户表,那就错了。 因为第一次迁移寻找用户表。 因此,您必须更改迁移顺序

app/database/migrations

目录

对我来说,问题是旧表使用的是 MyISAM 而不是 InnoDB。 这修复了它

    $tables = [
        'table_1',
        'table_2'
    ];

    foreach ($tables as $table) {
        \DB::statement('ALTER TABLE ' . $table . ' ENGINE = InnoDB');
    }

2021 年 4 月 20 日

在 Laravel 8 中,我遇到了这个问题。 如果您不使用nullable()则可能会发生此错误。

$table->bigInteger('user_id')->nullable()->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('set null');

就我而言,我在字符串user_id列上引用整数id列。 我变了:

$table->string('user_id')

到:

$table->integer('user_id')->unsigned();

希望它可以帮助某人!

要点是外来方法使用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 以匹配外键。

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

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

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

在我的情况下,直到我运行命令它才起作用

composer dump-autoload

这样你就可以将外键留在创建架构中

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');
}

对我来说,我的子表引用的表列没有编入索引。

Schema::create('schools', function (Blueprint $table) {
    $table->integer('dcid')->index()->unque();
    $table->integer('school_number')->index(); // The important thing is that this is indexed
    $table->string('name');
    $table->string('abbreviation');
    $table->integer('high_grade');
    $table->integer('low_grade');
    $table->timestamps();
    $table->primary('dcid');
});

Schema::create('students', function (Blueprint $table) {
      $table->increments('id');
      $table->integer('dcid')->index()->unique()->nullable();
      $table->unsignedInteger('student_number')->nullable();
      $table->integer('schoolid')->nullable();
      $table->foreign('schoolid')->references('school_number')->on('schools')->onDelete('set null');
      // ...
});

忽略可怕的命名,它来自另一个设计非常糟糕的系统。

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

希望有人可以帮助我入门。

(学习英语,抱歉)我在我的项目中尝试使用“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');
    });
}

重要提示:在这种情况下,首先创建没有外键的表,即“用户”表

另一个原因可能是数据库引擎类型。 Laravel 默认使用 MyISAM 引擎。 所以如果你需要强制执行外键引用检查,你应该使用 InnoDB 作为你的引擎。

这个设置在你的config/database.php文件中。 将引擎更改为'engine' => 'InnoDB'

此外,如果您尝试对现有表执行此操作,请使用以下方法更改引擎

DB::statement("ALTER TABLE `{$tableName}` ENGINE = 'InnoDB'");

我认为这里的答案缺少一件事,如果我错了,请更正我,但是外键需要在数据透视表上建立索引。 至少在mysql中似乎是这样。

public function up()
{
    Schema::create('image_post', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('image_id')->unsigned()->index();
        $table->integer('post_id')->unsigned()->index();
        $table->timestamps();
    });

    Schema::table('image_post', function($table) {
        $table->foreign('image_id')->references('id')->on('image')->onDelete('cascade');
        $table->foreign('post_id')->references('id')->on('post')->onDelete('cascade');
    });

}

制作数据透视表时,我在Laravel 5上遇到了相同的错误,而我的问题是我没有

->onDelete('cascade');

我认为:引用键必须是“索引”。 例如:(下)

public function up()
{
    Schema::create('clicks', function (Blueprint $table) {
        $table->increments('id');
        $table->string('viewer_id');
        $table->integer('link_id')->index()->unsigned();
        $table->string('time');
        $table->timestamps();
    });

    Schema::table('clicks', function($table) {
        $table->foreign('link_id')->references('id')->on('links')->onDelete('cascade');
    });


}

祝好运。

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

在自动创建的 create_users_table 迁移中更改$table->id(); $table->increments('id'); . 为我做的伎俩,希望这有帮助!

捷径解决方案:

//内部架构::创建

$table->foreign('user_id')->bigInteger('user_id')->unsigned()->index();

//内部架构::表

$table->foreign('users_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');

//处理你的代码。

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

  Schema::table('administrador', function($table) {
            $table->foreign('users_id')->references('id')->on('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');
;
        });

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM