繁体   English   中英

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

[英]how to use foreign key in laravel 5.1 migration

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

[Illuminate \\ Database \\ QueryException] SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:alter table samples add constraint s amples_supplier_id_foreign外键( supplier_id )引用suppliersid ))

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

样本迁移:

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,但再次失败

订单很重要。

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

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

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

...将架构代码添加到迁移文件中。 然后:

php artisan migrate

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

最后生成一个表的迁移记住,如果你觉得任何diffuculties只是命名你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');
        });

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

试试这种方式

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

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

暂无
暂无

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

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