首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 4架构生成器-循环参考

Laravel 4架构生成器-循环参考
EN

Stack Overflow用户
提问于 2015-03-16 18:43:02
回答 1查看 414关注 0票数 1

我有一个用户表和一个公司表。

User表有一个列'company_id‘,它引用Companies.id,公司有一个列'assignation’,它引用Users.id

显然,无论我首先尝试创建哪个表,我都会看到一个错误,它不能创建引用约束,因为另一个表不存在。

我可以手动完成此操作,但我正在寻找一种方法来使用手工迁移命令。

有办法解决这个问题吗?

这是我的代码:

代码语言:javascript
复制
Schema::create('companies', function($table) {

            $table->increments('id');
            $table->string('name');
            $table->integer('priority');
            $table->string('color');
            $table->string('shortname');

            $table->integer('assignation');

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

            $table->timestamps();

        });

Schema::create('users', function($table) {

            $table->increments('id');
            $table->string('username');
            $table->string('password');
            $table->string('email');
            $table->integer('security');
            $table->string('token');

            $table->integer('company_id');

            $table->foreign('company_id')->references('id')->on('companies');

            $table->timestamps();

        });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-16 19:07:30

你可以这样做:

代码语言:javascript
复制
Schema::create('companies', function($table) {

        $table->increments('id');
        $table->string('name');
        $table->integer('priority');
        $table->string('color');
        $table->string('shortname');

        $table->integer('assignation');

        $table->timestamps();

    });

Schema::create('users', function($table) {

        $table->increments('id');
        $table->string('username');
        $table->string('password');
        $table->string('email');
        $table->integer('security');
        $table->string('token');

        $table->integer('company_id');

        $table->foreign('company_id')->references('id')->on('companies');

        $table->timestamps();

    });

Schema::table('companies', function($table)
    {
        $table->foreign('assignation')->references('id')->on('users');
    });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29084575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档