【问题标题】:how to use foreign key in laravel 5.1 migration如何在 laravel 5.1 迁移中使用外键
【发布时间】:2016-03-11 02:17:28
【问题描述】:

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

[Illuminate\Database\QueryException] SQLSTATE[HY000]:一般错误: 1215 无法添加外键约束(SQL:alter table samples add 约束 amples_supplier_id_foreign 外键 (supplier_id) 参考suppliers (id))

[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


    【解决方案1】:

    订单很重要。

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

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

    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
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        这样试试

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

        【讨论】:

        • 我说这样做,但不成功
        • 先执行供应商迁移,然后进行样本迁移。原因没有供应商迁移,供应商id将无法作为外键
        【解决方案4】:

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

        改名前:

        供应商迁移:2015_08_21_104217_supplier_table.php

        样本迁移:2015_08_22_102325_samples_table.php

        改名后:

        样本迁移:2015_08_21_102325_samples_table.php

        供应商迁移:2015_08_22_104217_supplier_table.php

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

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

        【讨论】:

        • 我也一直在为这些事情苦苦挣扎。看看 laravelsd.com。您可以在那里创建一个数据库结构,然后将其全部导出。它创建迁移并将所有键绑定放在最后一个文件中。
        • 太棒了!是的,这也有效。我没有提到这种方法,因为我不是 laravel 专家,我不确定重命名迁移文件可能会出现什么问题。
        【解决方案5】:
        Schema::table('posts', function (Blueprint $table) {
            $table->unsignedInteger('user_id');
        
            $table->foreign('user_id')->references('id')->on('users');
        });
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
        【解决方案6】:

        每个人都是正确的,但最简单的方法是照常创建迁移文件。您将拥有例如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
        

        【讨论】:

          猜你喜欢
          • 2016-07-06
          • 2016-02-10
          • 2018-11-10
          • 2020-06-05
          • 2020-01-23
          • 2021-02-21
          • 2016-06-24
          • 2016-06-06
          • 2019-09-23
          相关资源
          最近更新 更多