Home  >  Q&A  >  body text

mysql - 请问数据库表中可以在一个表里面有两个关联同一个表的外键吗

具体是这样的:
我有一个用户表user,一个好友表fans。
fans中有三个字段 id,user_id,fans_id,其中user_id和fans_id都是跟user表的id关联的外键,我发现这样会报错。
可是用户跟好友的信息又要与用户表的信息关联,请问各位大神,怎样才不会报错或者有什么好一点的解决办法吗?

伊谢尔伦伊谢尔伦2731 days ago603

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-17 11:59:33

    See what error you reported.
    Under normal circumstances, there will be no problem if two fields in one table are foreign key related to the same field in another table at the same time.
    Unless the user_id in your fans table, the data type or size of the fans_id field does not match the id field of the user table; or the foreign key names are the same...

    reply
    0
  • Cancelreply