简体   繁体   中英

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. 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. 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. 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...

  • Every portfolio must be named.
  • Portfolio names are unique at the level of the user (but not globally: two portfolios can have same name if they belong to different users).
  • One stock can be in multiple portfolios.

...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.

StockID, PortfolioId, stockAttribute1, stockattribute2, ...

in the user table, userID is the primary key. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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