简体   繁体   English

Laravel 4-如何设置多个外键

[英]Laravel 4 - How to set up multiple foreign keys

I have 3 tables: member, team and teammember. 我有3张桌子:成员,团队和团队成员。 Each team can have multiple members, but they must not be repeated. 每个团队可以有多个成员,但不能重复。 Also, members can be in many teams at the same time. 此外,成员可以同时在多个团队中。

I want to setup the table teammember so that you can't add the same member to the table twice. 我想设置表团队成员,以便您不能将同一成员两次添加到表中。 In my migration I have this: 在迁移中,我有以下内容:

Schema::create('teammembers', function(Blueprint $table) {
    $table->increments('id');
    $table->integer('team_id');
    $table->integer('member_id');
}

I know how to setup the foreign keys by themselves, but in my case I need the restriction to only affect the combination of both keys, for the reasons explained before. 我知道如何自行设置外键,但是在我的情况下,我需要限制仅影响两个键的组合,原因如前所述。

$table->unique(['team_id','member_id'])将创建完整的唯一索引,但是仍然使用attach方法将尝试向数据透视表中添加新行,因此需要小心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM