如何使用外键laravel 5.1迁移

问题描述 投票:10回答:5

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

[照亮\数据库\ QueryException] SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:ALTER TABLE samples添加约束小号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 mysql laravel migration laravel-5.1
5个回答
7
投票

订购事宜。

你要确保你的“供应商”表试图在该表引用列,成为制约依旧存在。

所以,如果你想设置你的外键约束而创建表,然后确保你首先创建了“供应商”的迁移,以及“样本”迁移算账:

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

4
投票

最后生成的表迁移记住,他们应该是为了如果你觉得任何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');
        });

发生在去年和运行这里相关所有外键


1
投票

尝试这样的方式

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

1
投票

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

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

评论:我尝试用这种反射,即更名为任何地方所使用的迁移名


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

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

0
投票

每个人都是正确的但是最简单的方法是创建迁移文件如常。您将有例如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
© www.soinside.com 2019 - 2024. All rights reserved.