php 如何在 Laravel 5.1 迁移中使用外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34117302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:48  来源:igfitidea点击:

how to use foreign key in laravel 5.1 migration

phpmysqllaravelmigrationlaravel-5.1

提问by Abolfazl Yavari

I need to use foreign key for my database but I can't do this, after run migration command in command line, I get this error :

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

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

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

[Illuminate\Database\QueryException] SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter table samplesadd constraint samples_supplier_id_foreign外键(supplier_id)引用suppliersid))

[PDOException] SQLSTATE[HY000]:一般错误:1215 无法添加外键约束

Samples migration :

样品迁移:

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

Supplier migration :

供应商迁移:

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

I try to that with new migration for samples, but unsuccessful :

我尝试通过新的样本迁移来实现这一点,但没有成功:

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

I try to fix length of the primary key to 10, but unsuccessful again

我尝试将主键的长度固定为 10,但再次失败

采纳答案by KorreyD

Order matters.

订单很重要。

You want to be sure that your "suppliers" table exists before attempting to reference a column on that table as a constraint.

在尝试引用该表上的列作为约束之前,您希望确保您的“供应商”表存在。

So If you want to set your foreign key Constraint while creating your table then make sure that you create the "suppliers" migration first, and the "samples" migration afterwards:

因此,如果您想在创建表时设置外键约束,请确保先创建“供应商”迁移,然后创建“样本”迁移:

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

...add schema code to your migration files. and then:

...将架构代码添加到您的迁移文件。进而:

php artisan migrate


If you don't want to worry about the order in which the tables are created then do your create_table migrations first, without the foreign key constraints, and then do an additional migration to add your foreign keys.

如果您不想担心创建表的顺序,那么首先执行 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

...add schema code to your migration files. And then migrate using:

...将架构代码添加到您的迁移文件。然后使用以下方法迁移:

php artisan migrate

回答by ujwal dhakal

At last generate a migration for the table keep in mind that they should be in order if you feel any diffuculties just name ur table_foreign_keys

最后为表生成迁移请记住,如果您觉得有任何困难,请记住它们应该是有序的,只需命名您的 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');
        });

place all foreign keys related here at last and run

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

回答by Imtiaz Pabel

try like this way

像这样尝试

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

回答by Abolfazl Yavari

KorreyD say true ! , but I created migrations, and then renamed the migration to reorder them, It's so simple :

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

Before renamed :

改名前:

Supplier migration : 2015_08_21_104217_supllier_table.php

供应商迁移:2015_08_ 21_104217_supllier_table.php

Samples migration : 2015_08_22_102325_samples_table.php

样品迁移:2015_08_ 22_102325_samples_table.php

After renamed :

改名后:

Samples migration : 2015_08_21_102325_samples_table.php

样品迁移:2015_08_ 21_102325_samples_table.php

Supplier migration : 2015_08_22_104217_supllier_table.php

供应商迁移:2015_08_ 22_104217_supllier_table.php

my problem solved ! because supplier migration run before the samples migration

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

Comment : I try to this with reflector, that renamed any where that used migration name

评论:我尝试使用反射器,重命名任何使用迁移名称的地方

回答by Bashirpour

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

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

回答by dmgd

Everyone is correct however the easiest way is to create your migrations files a usual. You will have e.g. 2019_01_21_123456_create_table_one_table.php ...

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

I renamed them all

我把它们都重命名了

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

Now if I need to add a migration before table_two and after table_one I can simply change it to

现在,如果我需要在 table_two 之前和 table_one 之后添加迁移,我可以简单地将其更改为

2019_01_21_0015_create_table_five_table.php

Now the migration order will be

现在迁移顺序将是

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