MySQL: Two foreign keys in one table referring to another table

混江龙づ霸主 提交于 2019-12-02 00:54:45

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:
  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> 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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!