HABTM是否可以加入需求指数?(Does HABTM jointable needs index?)

系统教程 行业动态 更新时间:2024-06-14 16:57:18
HABTM是否可以加入需求指数?(Does HABTM jointable needs index?)

是否有必要在HABTM连接表上添加add_index? 我使用t.belongs_to(又名t.references)。 这是代码

class CreateCohortsUsersTable < ActiveRecord::Migration def change create_table :cohorts_users, :id => false do |t| t.belongs_to :cohort t.belongs_to :user end add_index :cohorts_users, :cohort_id # Do I need this? add_index :cohorts_users, :user_id # Do I need this? end end

Is it necessary to add_index on HABTM join table? I use t.belongs_to (aka t.references). Here is code

class CreateCohortsUsersTable < ActiveRecord::Migration def change create_table :cohorts_users, :id => false do |t| t.belongs_to :cohort t.belongs_to :user end add_index :cohorts_users, :cohort_id # Do I need this? add_index :cohorts_users, :user_id # Do I need this? end end

最满意答案

在两列上添加唯一索引通常是个好主意:

add_index :cohorts_users, [:cohort_id, :user_id], unique: true

It's usually a good idea to add a unique index on both columns:

add_index :cohorts_users, [:cohort_id, :user_id], unique: true

更多推荐

本文发布于:2023-04-12 21:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c142e867cd171c6c66782e1f081a048e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指数   需求   HABTM   jointable   index

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!