复合外键Laravel [英] Composite foreign key Laravel

查看:103
本文介绍了复合外键Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难创建复合外键.我想要一个既有"movieid"又有"cinemaid"作为复合外键的会话表.这是因为一个会话将同时需要电影和电影院位置.

I'm finding it difficult to create a composite foreign key. I would like a session table that has both 'movieid' and 'cinemaid' as a composite foreign key. This is because one session would require both the movie and the cinema location.

我当前的架构如下:

Schema :: create('session',function(Blueprint $ table){

Schema::create('session', function (Blueprint $table) {

$table->increments('id');
$table->integer('movieId');
$table->integer('cinemaId');
$table->foreign(array('movieId', 'cinemaId'))->references(array('id', 'id'))->on(array('movies', 'cinema'));
$table->dateTime('time');

});

我找不到有关如何在laravel中创建复合外键的更多信息.我发现的最好的东西:

I can't find much information on how to create composite foreign keys in laravel. The best thing I've found:

http://www.geexie.com/composite-primary-foreign -keys-laravel/

但是,在此示例中,它们从一个表中同时提取了两个外键,而在上面的示例中,您可以看到我需要从电影"表和电影"表中获取数据.我以为也许使用on(array('movies','cinema')可以工作,但是会出现错误数组到字符串转换".

However, in this example they pull both foreign keys from one table, where-as in my above example you can see that I need to get data from both the "movies" table and the "cinemas" table. I thought maybe using on(array('movies', 'cinema') would work, but it comes up with the error "Array to string conversion".

我尝试删除(array())部分,但这种方式也不起作用.

I've tried removing the (array()) part, but it doesn't work that way either.

如果我不打算使用复合外键,我也会很高兴听到任何其他解决方案.

I'd also be happy to hear any alternative solutions if I'm not suppose to be using composite foreign keys.

感谢一堆,

杰克.

推荐答案

外键链接到一个其他表.因此,您需要两个单独的外键,一个用于movies,一个用于cinema.

A foreign key links to one other table. So you need two separate foreign keys, one for movies and one for cinema.

$table->foreign('movieId')->references('id')->on('movies');
$table->foreign('cinemaId')->references('id')->on('cinema');

此外,我猜您想在session表中的两个字段movieIdcinemaId上使用一个复合索引.如果是这样,则需要确定是否将新的复合索引作为上的 primary 索引.

Also, I'm guessing you want a composite index in the session table, on two fields movieId and cinemaId. If so, you need to decide whether to have the new composite index as the primary index on session or not.

如果您希望复合索引成为您的主索引,那么您也不需要id字段,因此您需要删除$table->increments('id')行.您最终会得到这样的东西:

If you want the composite index to be your primary index, then you don't also need the id field, so you need to remove the $table->increments('id') line. You would end up with something like this:

Schema::create('session', function (Blueprint $table) {

$table->integer('movieId');
$table->integer('cinemaId');
$table->primary(['movieId', 'cinemaId']);  // note, this is a *primary* key
$table->foreign('movieId')->references('id')->on('movies');
$table->foreign('cinemaId')->references('id')->on('cinema');
$table->dateTime('time');

});

或者,如果要保留id作为主要索引,则只希望复合索引成为常规的旧索引.因此,您可能会执行以下操作:

Or, if you want to keep the id as the primary index, then you just want the composite index to be a regular old index. So you might do something like this:

Schema::create('session', function (Blueprint $table) {

$table->increments('id');
$table->integer('movieId');
$table->integer('cinemaId');
$table->index(['movieId', 'cinemaId']);  // This is an index, but *not* a primary key
$table->foreign('movieId')->references('id')->on('movies');
$table->foreign('cinemaId')->references('id')->on('cinema');
$table->dateTime('time');

});

有帮助吗?

这篇关于复合外键Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆