如何在 laravel 5.1 迁移中使用外键

how to use foreign key in laravel 5.1 migration

我需要为我的数据库使用外键,但我不能这样做,在命令行中执行 运行 迁移命令后,我收到此错误:

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table samples add constraint s amples_supplier_id_foreign foreign key (supplier_id) references suppliers (id))

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

示例迁移:

Schema::create('samples', function (Blueprint $table) {
            $table->Increments('id',true);
            $table->string('variety',50);
            $table->integer('supplier_id')->unsigned();
            $table->foreign('supplier_id')->references('id')->on('suppliers');
            $table->string('lot_number');
            $table->date('date');
            $table->integer('amount');
            $table->integer('unit_id')->unsigned();
            $table->foreign('unit_id')->references('id')->on('unit');
            $table->string('technical_fact');
            $table->string('comments');
            $table->string('file_address');
            $table->integer('category_id')->unsigned();
            $table->foreign('category_id')->references('id')->on('category');
            $table->timestamps();
        });

供应商迁移:

Schema::create('suppliers', function (Blueprint $table) {
            $table->Increments('id',true);
            $table->string('supplier',50);
            $table->timestamps();
        });

我尝试对样本进行新的迁移,但没有成功:

Schema::create('samples', function (Blueprint $table) {
                $table->Increments('id',true);
                $table->string('variety',50);
                $table->integer('supplier_id')->unsigned();
                $table->string('lot_number');
                $table->date('date');
                $table->integer('amount');
                $table->integer('unit_id')->unsigned();
                $table->string('technical_fact');
                $table->string('comments');
                $table->string('file_address');
                $table->integer('category_id')->unsigned();
                $table->timestamps();
        });

        Schema::table('samples', function($table) {
            $table->foreign('supplier_id')->references('id')->on('suppliers');
            $table->foreign('unit_id')->references('id')->on('unit');
            $table->foreign('category_id')->references('id')->on('category');
        });

我尝试修改主键长度为10,但还是失败了

这样试试

Schema::table('samples', function($table) {
        $table->integer('supplier_id')->unsigned();
        $table->foreign('supplier_id')->references('id')->on('suppliers');
        $table->integer('unit_id')->unsigned();
        $table->foreign('unit_id')->references('id')->on('unit');
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('category');
    });

最后为 table 生成迁移请记住,如果您觉得有任何困难,请记住它们应该是有序的,只需命名您的 table_foreign_keys

 Schema::table('samples', function($table) {
            $table->foreign('supplier_id')->references('id')->on('suppliers');
            $table->foreign('unit_id')->references('id')->on('unit');
            $table->foreign('category_id')->references('id')->on('category');
        });

最后把所有相关的外键放在这里运行

订单很重要。

在尝试引用 table 上的列作为约束之前,您需要确保 "suppliers" table 存在。

因此,如果您想在创建 table 时设置外键约束,请确保先创建“suppliers”迁移,然后再创建“样本”之后的迁移:

php artisan make:migration create_suppliers_table --create=suppliers
php artisan make:migration create_samples_table --create=samples

...将模式代码添加到您的迁移文件中。然后:

php artisan migrate

如果您不想担心 table 的创建顺序,那么首先进行 create_table 迁移,没有外键约束,然后进行额外的迁移添加外键。

php artisan make:migration create_samples_table --create=samples
php artisan make:migration create_suppliers_table --create=suppliers
php artisan make:migration alter_samples_table --table=samples   <-- add your foreign key constraints to this migration file

...将模式代码添加到您的迁移文件中。然后迁移使用:

php artisan migrate

KorreyD 说的对! , 但我创建了迁移,然后重命名迁移以重新排序它们,就这么简单:

改名前:

供应商迁移:2015_08_21_104217_supllier_table.php

示例迁移:2015_08_22_102325_samples_table.php

改名后:

示例迁移:2015_08_21_102325_samples_table.php

供应商迁移:2015_08_22_104217_supllier_table.php

我的问题解决了!因为供应商迁移 运行 在样品迁移

之前

评论:我尝试使用反射器进行此操作,它重命名了任何使用迁移名称的地方

Schema::table('posts', function (Blueprint $table) {
    $table->unsignedInteger('user_id');

    $table->foreign('user_id')->references('id')->on('users');
});

每个人都是正确的,但最简单的方法是像往常一样创建迁移文件。你会有例如2019_01_21_123456_create_table_one_table.php ...

我都重命名了它们

2019_01_21_0010_create_table_one_table.php
2019_01_21_0020_create_table_two_table.php
2019_01_21_0030_create_table_three_table.php
2019_01_21_0040_create_table_four_table.php

现在,如果我需要在 table_two 之前和 table_one 之后添加迁移,我只需将其更改为

2019_01_21_0015_create_table_five_table.php

现在迁移顺序为

2019_01_21_0010_create_table_one_table.php
2019_01_21_0015_create_table_five_table.php
2019_01_21_0020_create_table_two_table.php
2019_01_21_0030_create_table_three_table.php
2019_01_21_0040_create_table_four_table.php