开发者

DB schema problem

开发者 https://www.devze.com 2023-03-30 09:20 出处:网络
I have tables USERS, CLUBS (id, creator_id, name, description) MONOPOLY (id, results) RISK (id, results, risk_specific_field)

I have tables

  • USERS,
  • CLUBS (id, creator_id, name, description)
  • MONOPOLY (id, results)
  • RISK (id, results, risk_specific_field)

Relations are:

  • USERS n:n CLUBS // I have weak entity between them
  • CLUBS 1:n MONOPOLY
  • CLUBS 1:n RISK

Users create clubs of friends and invite them to that club to play different games (Monopoly, RIsk).

If the game is e.g. monopoly then there should be relation between Game and Monopoly table. I thought creating weak entities between Games/Monopoly and Games/Risk which would work, but I am confused whether this approach is good because weak entities are used to break N:N relations and these ones are 1:n (one game relates to 1 row of Monopoly table and Monopoly to N rows of Game table). Also this approach would allow our app to add new types of games with their own tables etc.

If the only t开发者_Go百科ype of game was Monopoly I would add field id_monopoly in my Games table and that would be it, but since my app has Risk also I can't do it this way.

Hell, I hope I was clear about what concerns me here :)


DB schema problem


If I understand correctly, what you are looking for is polymorphic associations.

Instead of adding just id_monopoly, you would generalize it and add two fields, such as:

id_game, type_game - where id_game is either a "foreign key" to monopoly or to risk and type_game is either "monopoly" or "risk".

The catch here is that you will need some application logic to look up the ids in the proper table, which can get complicated, but hope this helps in your case.

0

精彩评论

暂无评论...
验证码 换一张
取 消