简体   繁体   English

如何为不同类型的用户设计数据库表?

[英]How to design database tables for different types of Users?

I'm on a project with Rails, Postgresql and Active Record, where I have Users, that can be either Influencers, or Creators.我正在使用 Rails、Postgresql 和 Active Record 进行一个项目,我有用户,可以是影响者,也可以是创作者。

The Users have common columns such as email , password , first_name and last_name , but:用户有常见的列,例如emailpasswordfirst_namelast_name ,但是:

  • influencers will have followers , eg_rate , account columns影响者将有followerseg_rateaccount
  • creators will have SIRET_number and specialty columns创作者将拥有SIRET_numberspecialty

How can I design my database so Influencers and Creators are kind of "child" of the Users table?如何设计我的数据库,使影响者和创作者成为用户表的“孩子”? I mean, is it possible to have a db where I can access a User' followers or a User's specialty with one query, or I'll always have to do multiple queries to achieve this?我的意思是,是否有可能有一个数据库,我可以通过一个查询访问用户的关注者或用户的专业,或者我总是需要进行多个查询才能实现这一点? I've tried to create three tables for each, with a foreign key user_id in Influencers table and Creators table.我尝试为每个表创建三个表,在 Influencers 表和 Creators 表中都有一个外键user_id I also tried to add a user_type column to my User table where I pass the values of either "influencer" or "creator", but I'm kind of lost on how to link every tables...我还尝试在我的用户表中添加一个user_type列,在其中传递“影响者”或“创建者”的值,但我对如何链接每个表有点迷失......

Thank you everyone.谢谢大家。

Your approach is right.你的方法是对的。

You can create a table users with the common columns and add a foreign key to influencers and creators tables.您可以使用公共列创建表users并向influencers者和creators表添加外键。

Then when you need to retrieve the data, you can use ActiveRecord relations to easily fetch data and use ActiveRecord's includes method for relations.然后当你需要检索数据时,你可以使用 ActiveRecord 关系来轻松获取数据并使用 ActiveRecord 的include方法进行关系。

For example:例如:

class Creator < ActiveRecord::Base
   # The relation must be set
   has_one :user
end

# To fetch data anywhere else:
Creator.find_by(SIRET_number: 1234).includes(:user)

If you need to retrieve a creator or influencer by an attribute from related users table, you can use joins :如果您需要通过相关用户表中的属性检索创建者或影响者,您可以使用joins

Creator.joins(:users).where(users: {email: "foo@bar.com"})

Make sure you have the relations set in both User and Creator models.确保在 User 和 Creator 模型中都设置了关系。 Check out this topic for more info.查看此主题以获取更多信息。

By using includes or joins instead of using creator.user you'll avoiding the unnecessary additional query.通过使用包含或连接而不是使用creator.user ,您将避免不必要的额外查询。 The downside is the syntax is now longer, you can maybe create your own getters to easily retrieve data instead of writing "includes" everytime.缺点是语法现在更长了,您也许可以创建自己的 getter 来轻松检索数据,而不是每次都编写“包含”。

Also assuming you're not aware of this method, I suggest you to read about the common N+1 problem with Rails & ActiveRecord.另外假设您不知道这种方法,我建议您阅读有关 Rails 和 ActiveRecord 的常见 N+1 问题。 The same methods can solve a lot of problems for you.同样的方法可以为你解决很多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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