[英]How should I design the database relationship in this situation
So I want a user to have multiple portfolios, and in the portfolio, it can contain multiple stocks. 因此,我希望用户拥有多个投资组合,并且在投资组合中,它可以包含多个股票。 It should be as straight forward as a has_many though relationship.
它应该和has_many关系一样简单。 But I also want to user to be able to name the portfolios, Since in the portfolio model, we would have two columns, user_id and stock_id.
但是我也希望用户能够命名投资组合,因为在投资组合模型中,我们将有两列,user_id和stock_id。 Where should I fit the "name" attribute to the model?
我应该在哪里将“名称”属性适合模型? if I have a third column called "name", wouldn't that be kind of redundant?
如果我有第三列称为“名称”,那不是多余的吗? Because, say user 1 has portfolio 1 which contains stock 3,4,5.
因为,例如,用户1具有包含股票3、4、5的投资组合1。 This would consists of three rows, like
这将包括三行,例如
user_id | stock_id | name
---------+----------+------------
1 | 3 | portfolio1
1 | 4 | portfolio1
1 | 5 | portfolio1
What will be the better solution to design this relationship? 设计这种关系的更好的解决方案是什么?
Thanks 谢谢
It seems like your Portfolio model is misnamed. 您的投资组合模型似乎命名错误。 To my mind your models should look more like this:
在我看来,您的模型应该更像这样:
class User < ActiveRecord::Base
has_many :portfolios
end
class Portfolio < ActiveRecord::Base
belongs_to :user
has_many :stocks, :through => :portfolios_stocks # join table
# has the `name` attribute
end
class Stock < ActiveRecord::Base
has_many :portfolios, :through => :portfolios_stocks
end
Assuming the following requirements... 假设以下要求...
...this database model should get you started: ...此数据库模型应该可以帮助您入门:
BTW, this model uses natural keys approach. 顺便说一句,此模型使用自然键方法。 You could also use surrogate keys, for example:
您还可以使用代理键,例如:
Each has pros and cons, but this is a different topic... 每个人都有优点和缺点,但这是一个不同的主题...
From your description: user to have multiple portfolios, and in the portfolio, it can contain multiple stocks 根据您的描述:用户拥有多个投资组合,并且在投资组合中,它可以包含多个股票
you should have one table for the user 您应该为用户提供一张桌子
UserID, UserAttribute1, UserAttribute2, ...
one table for portfolios PortfolioID, UserID, portfolioAttribute1, portfolioAttribute2, ...
and one table for stocks. UserID, UserAttribute1, UserAttribute2, ...
用于投资PortfolioID, UserID, portfolioAttribute1, portfolioAttribute2, ...
一个表PortfolioID, UserID, portfolioAttribute1, portfolioAttribute2, ...
和一个用于股票的表。
StockID, PortfolioId, stockAttribute1, stockattribute2, ...
in the user table, userID is the primary key. 在用户表中,userID是主键。 in the portfolio table, portfolioID is the primary key, with the UserID being a foreign key so you can link the portfolios back to the users.
在投资组合表中,PortfolioID是主键,而UserID是外键,因此您可以将投资组合链接回用户。
for further reading, you can research Database normalization 为了进一步阅读,您可以研究数据库规范化
User Table 用户表
id
Stock Table 库存表
id | portfolio_id
Portfolio Table 投资组合表
id | user_id | name
This is one way to organize your relationships. 这是组织关系的一种方式。 Each user has many portfolios and each stock belongs to a single portfolio.
每个用户都有许多投资组合,每只股票都属于一个投资组合。 This gives you to the ability to change portfolios between users without also having to modify stock data.
这使您能够更改用户之间的投资组合,而不必修改库存数据。
Keep in mind there are other ways of organizing your relationships depending on what you consider to be more important. 请记住,还有其他方法可以组织关系,这取决于您认为更重要的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.