简体   繁体   English

MySQL:一个表中的两个外键引用另一个表

[英]MySQL: Two foreign keys in one table referring to another table

I've come across something that seemed simple before but has me scratching my head again. 我曾经遇到过一些看似简单的事情,但是又让我挠头。 I have a table for users: 我有一个供用户使用的表:

user_id (PK) | username| email | something

... and a table for "views" for when one user has viewed another user: ...以及一个用户观看另一用户时的“观看次数”表:

view_id (PK) | viewer_id | viewed_id | view_date

The "viewer_id" and "viewed_id" are both user_ids, allowing me to search separately for instances when a user was the viewer or the one being viewed. “ viewer_id”和“ viewed_id”都是user_id,允许我分别搜索某个用户是查看者还是正在查看的实例。

I initially thought that both of these columns would be foreign keys, but having created the tables in my schema.yml file (I'm using Doctrine 1.2) and specified two separate foreign relationships (one for each column), it seems Doctrine only takes into account the first listed foreign relationship between these two tables (user_id > viewer_id). 我最初以为这两列都是外键,但是在我的schema.yml文件中创建了表(我使用的是Doctrine 1.2)并指定了两个单独的外关系(每列一个),看来Doctrine只需要考虑到这两个表之间第一个列出的外部关系(user_id> viewer_id)。

It's got me confused now whether this is correct MySQL behaviour, a problem in Doctrine, or a problem in the way I'm approaching this, or nothing to worry about! 现在让我感到困惑的是,这是否是正确的MySQL行为,Doctrine中的问题,或者我正在解决此问题的方式中的问题,或者没有什么可担心的! Can there be two separate foreign keys from one table mapped to the same column in another table? 一个表中是否可以有两个单独的外键映射到另一表中的同一列? Is it even logical, given that a JOIN would still give me access to "views" through a user_id? 考虑到JOIN仍然可以让我通过user_id访问“视图”,这是否甚至合乎逻辑? Have I got it wrong? 我看错了吗?

Thanks for your time. 谢谢你的时间。

EDIT - The schema file: 编辑-模式文件:

User:
relations:
View: {class: View, local: user_id, foreign: viewer_id, type: many, foreignType: one, alias: View, foreignAlias: User}
View: {class: View, local: user_id, foreign: viewed_id, type: many, foreignType: one, alias: View, foreignAlias: User}

... only difference is viewer_id/viewed_id

And here we go: You specified the same aliases for the relations. 然后我们开始:您为关系指定了相同的别名。

User:
  relations:
    viewed_by: 
       class: View
       local: user_id
       foreign: viewed_id
       type: many
       foreignType: one
       foreignAlias: viewed

    viewed:
      class: View
      local: user_id
      foreign: viewer_id
      type: many
      foreignType: one
      foreignAlias: viewer

Or you set up the whole many-to-many relation differently: 或者您以不同的方式建立整个多对多关系:

User:
   relations:
     viewed_by: 
       class: User 
       local: viewed_id
       foreign: viewer_id,
       refClass: View
     viewed:
       class: User
       local:viewer_id
       foreign: viewed_id
       refClass: View

and View should look like View应该看起来像

View:
  columns:
    viewed_id:
      type: integer
      primary: true
    viewer_id:
      type: integer
      primary: true

See the Doctrine documentation on many-to-many relationships . 有关多对多关系的信息,请参阅《教义》文档。

Mysql allows multiple foreign keys on the same table, even to the same column in another table, but I cannot tell why doctrine only creates one of them: Mysql允许在同一张表上使用多个外键,甚至允许在另一个表中的同一列上使用,但我不能说为什么主义只能创建其中一个:

mysql> CREATE TABLE test1 (id INT);
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE test3 (ref1 INT, ref2 INT, FOREIGN KEY (ref1) REFERENCES test1(id), FOREIGN KEY (ref2) REFERENCES test1(id));
Query OK, 0 rows affected (0.01 sec)

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

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