Ruby on rails 如何在rails中正确维护数据库模式?
我有这样的模式:Ruby on rails 如何在rails中正确维护数据库模式?,ruby-on-rails,ruby-on-rails-5,database-schema,table-relationships,Ruby On Rails,Ruby On Rails 5,Database Schema,Table Relationships,我有这样的模式: create_table "grades", force: :cascade do |t| t.integer "cls" t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "post_grades", force: :cascade do |t| t.integer "post_id" t.int
create_table "grades", force: :cascade do |t|
t.integer "cls"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "post_grades", force: :cascade do |t|
t.integer "post_id"
t.integer "grade_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.integer "user_id", null: false
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "user_grades", force: :cascade do |t|
t.integer "user_id"
t.integer "grade_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
在这个模式中,帖子和用户都是相互关联的
所以我的第一个问题是:这样做正确吗
假设一个用户在他的列中添加了等级4(cls=4),另一个用户添加了相同的等级。现在,对于两个不同的等级ID,我在等级表中有相同的cls值。因此,此模式中是否存在任何数据冗余?当我收到您的任务时,您可以使用以下解决方案:
等级
->无参考资料(目前的情况)user\u grades
(此表的传统命名方式为grades\u users
…字母顺序和复数)->user\u id
,grades\u id
(现在是这样)post\u分数
。这是多余的posts
->grade\u id
,用户id
(如果您需要作者)schema.rb
看起来像:
create_table "users", force: :cascade do |t|
...
end
create_table "grades_users", force: :cascade do |t|
t.integer "user_id"
t.integer "grade_id"
...
end
create_table "grades", force: :cascade do |t|
...
end
create_table "posts", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "grade_id", null: false
...
end
你能描述一下你们桌子的逻辑吗?如果没有任何概念上的理解,很难说出有用的东西。语言学家有很多等级,可以在这些等级中发布帖子。职系可以有多个职位,一个职位只能在一个职系中。一个等级可以有许多用户。是否足够或需要详细说明?是否
Grade
有任何特定的用户,如创建者/etc或他们都是相似的?什么是cls
?是的,您的回答让事情变得有点清楚,但仍然需要更多的信息来帮助您是的,在这个用户中有许多角色,如管理员、教师和学生,其中只有管理员和教师可以在年级中发布帖子。假设一个用户在他的专栏中添加了年级(cls:4),另一个用户添加了相同的年级。现在我在两个不同的等级ID的等级表中有相同的cls值。那么这也是数据冗余吗?不,这不是数据冗余,因为按照这种方法,所有重复的数据都是冗余的。您是否需要仅使用唯一的cls
字段创建Grade
?不需要。对于特定用户。仅使用唯一的cls字段创建成绩。当然,不同的用户可以拥有与其他用户相同的cls字段。然后一切正常。请让我们回到你最初的问题。现在这已经是一个新问题了。我的回答对你有帮助吗?如果是,请在上面打勾并投票,以便以后搜索。失去了事先的感谢。