Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 新的Laravel项目一次添加多个带有外键的表_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 新的Laravel项目一次添加多个带有外键的表

Php 新的Laravel项目一次添加多个带有外键的表,php,laravel,laravel-5,Php,Laravel,Laravel 5,我启动了一个运行5.6的新laravel应用程序,并在运行migrate之前一次创建了多个迁移,以使流程流线型化。 我有大量外键用于链接,使我的数据库易于扩展 我的问题是尝试引用迁移中稍后出现的数据库中的外键。我认为它不起作用,因为我试图引用的表尚未按操作顺序创建 例: 创建用户表迁移: $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table-&

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

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

例:
创建用户表
迁移:

$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();
            $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();
例如:
create\u types\u table
migration:

$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();
            $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();
我在所有的桌子上都遵循同样的模式。 我想我的问题是;这是因为表还没有创建,所以没有可引用的内容吗?有没有办法解决这个问题,或者我需要重新启动,创建所有表,然后为每个表添加另一个迁移,以添加外键


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

您不能引用尚未创建的表。您可以执行单独的
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');
    });
}

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

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

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

另外,将外键关系放在两个表上是错误的,除非您有多对多关系

建立关系时:
  • 外键和主键必须相同(
    increments()
    表示它是整数、无符号和自动递增)
  • 您无法与不存在的表建立关系(在Laravel中,当您运行
    php artisan migrate
    时,它会读取从最旧到最新的[按创建日期]的迁移)
  • $table->foreign('type_id')->引用('id')->on('types')Users
    表中的code>将其引用到
    Types
    表的主键,在创建新迁移时,我假定该主键为
    id
    laravel default
  • 这意味着当它运行时:

    $table->integer('type_id')->unsigned();
    $table->foreign('type_id')->references('type_id')->on('types');
    
    它试图引用您刚刚在
    types
    表中使用column
    type\u id
    创建的
    type\u id
    列,在这种情况下,它不存在,因为它是从create\u users\u表迁移开始的

    我的解决方案:
    从所有表中删除所有关系,只创建将作为外键的列,如,
    $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');
    });
    // ....
    
    这样,您可以创建所有表,然后在单独的表中分配外键
    迁移。

    您可以执行多次迁移,也可以将迁移分解为多个
    Schema::create
    /
    Schema::table
    块。您不能引用尚未创建的表。但是如果看不到实际错误,就很难判断这是否是问题所在。如果
    创建用户\u表
    创建类型\u表
    之前运行,您肯定会收到引用不存在的表的错误(
    类型
    )。