【问题标题】:New Laravel Project adding multiple tables with foreign keys at once新的 Laravel 项目一次添加多个带有外键的表
【发布时间】:2018-07-24 13:49:39
【问题描述】:

我启动了一个运行 5.6 的新 laravel 应用程序,并简化了我在运行 migrate 之前一次创建多个迁移的过程。 我有大量用于链接的外键,以使我的数据库易于扩展。

我的问题是尝试在迁移后期出现的数据库中引用外键。我相信它不起作用,因为我试图引用的表尚未按操作顺序创建。

例如: create_users_table迁移:

$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');

$table->integer('user_id')->nullable()->unsigned();
$table->foreign('user_id')->references('id')->on('users');

$table->integer('type_id')->nullable()->unsigned();
$table->foreign('type_id')->references('type_id')->on('types');

$table->integer('post_id')->nullable()->unsigned();
$table->foreign('post_id')->references('post_id')->on('posts');

$table->rememberToken();
$table->timestamps();

例如:create_types_table 迁移:

            $table->increments('id');
            $table->string('type');

            $table->integer('user_id')->nullable()->unsigned();
            $table->foreign('user_id')->references('user_id')->on('users')->nullable();

            $table->integer('revenue_id')->nullable()->unsigned();
            $table->foreign('revenue_id')->references('revenue_id')->on('revenue')->nullable();

            $table->integer('hours_id')->nullable()->unsigned();
            $table->foreign('hours_id')->references('hours_id')->on('hours')->nullable();

            $table->integer('project_id')->nullable()->unsigned();
            $table->foreign('project_id')->references('project_id')->on('projects')->nullable();

            $table->integer('type_id')->nullable()->unsigned();
            $table->foreign('type_id')->references('id')->on('types');
            $table->timestamps();

我在所有表格中都遵循相同类型的模式。 我想我的问题是;这是因为表还没有创建,所以没有什么可以参考的吗?有没有办法解决这个问题,还是我需要重新启动,创建所有表,然后为每个表添加另一个迁移以添加外键?

这可能是一个愚蠢的问题,我对 laravel 还很陌生。这是我第一次尝试这么早地将关系添加到应用程序中。

【问题讨论】:

  • 您可以进行多次迁移,也可以将您的迁移分成多个 Schema::create / Schema::table 块。
  • 您不能引用尚未创建的表。但是如果没有看到实际的错误,很难判断这是否是问题所在。
  • 如果create_users_tablecreate_types_table之前运行,你肯定会收到一个引用不存在的表的错误(types)。

标签: php laravel laravel-5


【解决方案1】:

您不能引用尚未创建的表。您可以单独执行Schema::create/Schema::table(正如@ceejayoz 所说)来实现这一点,因此您的所有配置都放在一个迁移文件中:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('user_id');
        // the rest of the fields
        $table->timestamps();
    });

    Schema::create('some-other-table', function (Blueprint $table) {
        $table->increments('other_table_id');
        // the rest of the fields
        $table->timestamps();
    });

    Schema::table('some-other-table', function (Blueprint $table) {
        $table->foreign('some_field_id')->references('user_id')->on('users');
    });
}

【讨论】:

    【解决方案2】:

    由于您无法引用不存在的表,您可以尝试创建所有不带属性的表,例如。 :

    Schema::create('table1', function (Blueprint $table) {
        $table->increments('table1_id');
        $table->int('table1_int');
    });
    Schema::create('table2', function (Blueprint $table) {
        $table->increments('table2_id');
        $table->int('table2_int');
    });
    Schema::create('table3', function (Blueprint $table) {
        $table->increments('table3_id');
        $table->int('table3_int');
    });
    

    然后您可以分配外键约束和关系,例如:

    Schema::table('table2', function (Blueprint $table) {
        $table->foreign('some_field_id')->references('table1_id')->on('table1');
    });
    Schema::table('table3', function (Blueprint $table) {
        $table->foreign('some_field_id')->references('table2_id')->on('table2');
    });
    

    【讨论】:

      【解决方案3】:

      外键及其关系不能为空,请确保删除->nullable()

      $table->unsignedInteger('user_id');
      

      另外,你在两个表上都放置了外键关系,这是错误的,除非你有一个多对多关系Learn more about it

      建立关系时:

      1. 外键和主键必须相同(increments() 表示它是整数、无符号且自动递增)。
      2. 您不能与不存在的表建立关系(在 Laravel 中,当您运行 php artisan migrate 时,它会读取从最旧到最新的迁移 [按创建日期])。
      3. Users 表中的 $table->foreign('type_id')->references('id')->on('types'); 将其引用到 Types 表的主键,我假设在您创建新迁移时 Laravel 默认为 id

      这意味着当它运行时:

      $table->integer('type_id')->unsigned();
      $table->foreign('type_id')->references('type_id')->on('types');
      

      它试图引用您刚刚使用types 表中的type_id 列创建的type_id 列,在这种情况下它不存在,因为它是从 create_users_table 迁移开始的。

      我的解决方案:
      从所有表中删除所有关系,只创建将作为外键的列,如$table->unsignedInteger('type_id');
      在终端中运行php artisan make:migration create_foreign_keys,打开然后放入:

      Schema::table('users', function (Blueprint $table) {
          $table->foreign('type_id')->references('id')->on('types');
      });
      Schema::table('types', function (Blueprint $table) {
          $table->foreign('whatever_id')->references('id')->on('whatever_table');
      });
      // ....
      

      这样你创建了所有的表,然后将外键分配到一个单独的 迁移。

      【讨论】:

        猜你喜欢
        • 2014-12-24
        • 1970-01-01
        • 2021-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-28
        • 2019-10-15
        相关资源
        最近更新 更多