Rails: create_join_table์—๋Š” ์ธ๋ฑ์Šค์™€ ์™ธ๋ž˜ ํ‚ค ์ œ์•ฝ ์กฐ๊ฑด์ด ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2016๋…„ 01์›” 07์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: rails/rails

ํ˜„์žฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ์ธ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ:

rails g migration create_join_table_showroom_user showroom user

๋‹ค์Œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      # t.index [:showroom_id, :user_id]
      # t.index [:user_id, :showroom_id]
    end
  end
end

์ด ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์„ ์ง์ ‘ ์‹คํ–‰ํ•˜๋ฉด ๋งค์šฐ ๊ฐ„๋‹จํ•œ ์กฐ์ธ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

create_table "showrooms_users", id: false, force: :cascade do |t|
  t.integer "showroom_id", null: false
  t.integer "user_id",     null: false
end

๋”ฐ๋ผ์„œ ์ธ๋ฑ์Šค, ์™ธ๋ž˜ ํ‚ค ๋“ฑ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์—์„œ ๋‘ ์ค„์˜ ์ฃผ์„ ์ฒ˜๋ฆฌ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ์ด์ œ ๋‹ค์Œ์„ ์–ป์Šต๋‹ˆ๋‹ค.

create_table "showrooms_users", id: false, force: :cascade do |t|
  t.integer "showroom_id", null: false
  t.integer "user_id",     null: false
end

add_index "showrooms_users", ["showroom_id", "user_id"], name: "index_showrooms_users_on_showroom_id_and_user_id", using: :btree
add_index "showrooms_users", ["user_id", "showroom_id"], name: "index_showrooms_users_on_user_id_and_showroom_id", using: :btree

์ƒ์„ฑ๋œ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      # t.references :showroom, index: true, null: false, foreign_key: true
      # t.references :user, index: true, null: false, foreign_key: true
      # t.index [:showroom_id, :user_id]
      # t.index [:user_id, :showroom_id]
    end
  end
end

๋‘ ๊ฐœ์˜ references ํ–‰์˜ ์ฃผ์„ ์ฒ˜๋ฆฌ๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ์ƒ์„ฑ๋œ ํ…Œ์ด๋ธ”์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.

create_table "showrooms_users", id: false, force: :cascade do |t|
  t.integer "showroom_id", null: false
  t.integer "user_id",     null: false
end

add_index "showrooms_users", ["showroom_id"], name: "index_showrooms_users_on_showroom_id", using: :btree
add_index "showrooms_users", ["user_id"], name: "index_showrooms_users_on_user_id", using: :btree

add_foreign_key "showrooms_users", "showrooms"
add_foreign_key "showrooms_users", "users"

๋ˆ„๊ตฐ๊ฐ€๊ฐ€ ์กฐ์ธ ํ…Œ์ด๋ธ”์—์„œ ์™ธ๋ž˜ ํ‚ค์™€ ์ธ๋ฑ์Šค๋ฅผ ์›ํ•˜์ง€ ์•Š๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ? @rafaelfranca

activerecord

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์€ ๋Œ€๋ถ€๋ถ„ ๊ทธ๋Ÿฐ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. :index ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ฌ๋ฆฌ๋‹ค

rails g migration create_join_table_showroom_user showroom:index user:index

์ƒ์‚ฐํ•˜๋‹ค

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      t.index [:showroom_id, :user_id]
      t.index [:user_id, :showroom_id]
    end
  end
end

๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ๋•Œ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ:

rails g migration CreatePost title:string user:references:index

๋‹ค์Œ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

class CreatePost < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.references :user, index: true, foreign_key: true
    end
  end
end

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค.

rails g migration create_join_table_showroom_user showroom:references:index user:references:index

์ด๊ฒƒ์„ ๋งŒ๋“ค๊ธฐ:

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      t.references :showroom, index: true, foreign_key: true
      t.references :user, index: true, foreign_key: true
      # t.index [:showroom_id, :user_id]
      # t.index [:user_id, :showroom_id]
    end
  end
end

๊ทธ๋Ÿฌ๋ฉด ๊ธฐ๋ณธ rails g migration create_join_table_showroom_user showroom user ๋Š” ๋‹ค์Œ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      # t.references :showroom, index: true, foreign_key: true
      # t.references :user, index: true, foreign_key: true
      # t.index [:showroom_id, :user_id]
      # t.index [:user_id, :showroom_id]
    end
  end
end

๋ชจ๋“  3 ๋Œ“๊ธ€

๋‚˜๋Š” ๋™์˜ํ•œ๋‹ค! ๋‚˜๋Š” ํ•ญ์ƒ ์ด๊ฒƒ์„ ํ•ด์•ผ ํ•œ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ๊ธฐ์— ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
rails g migration ... --fk --idx
๋˜๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋น„ํ™œ์„ฑํ™”ํ•˜๋Š” ์˜ต์…˜์ด ์žˆ์Šต๋‹ˆ๋‹ค.
rails g migration ... --no_fk --no_idx

๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์€ ๋Œ€๋ถ€๋ถ„ ๊ทธ๋Ÿฐ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. :index ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ ์ธ๋ฑ์Šค๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋‹ฌ๋ฆฌ๋‹ค

rails g migration create_join_table_showroom_user showroom:index user:index

์ƒ์‚ฐํ•˜๋‹ค

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      t.index [:showroom_id, :user_id]
      t.index [:user_id, :showroom_id]
    end
  end
end

๊ทธ๋ฆฌ๊ณ  ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ๋•Œ ์ฐธ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ:

rails g migration CreatePost title:string user:references:index

๋‹ค์Œ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

class CreatePost < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title
      t.references :user, index: true, foreign_key: true
    end
  end
end

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒƒ์„ ํ—ˆ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์Šต๋‹ˆ๋‹ค.

rails g migration create_join_table_showroom_user showroom:references:index user:references:index

์ด๊ฒƒ์„ ๋งŒ๋“ค๊ธฐ:

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      t.references :showroom, index: true, foreign_key: true
      t.references :user, index: true, foreign_key: true
      # t.index [:showroom_id, :user_id]
      # t.index [:user_id, :showroom_id]
    end
  end
end

๊ทธ๋Ÿฌ๋ฉด ๊ธฐ๋ณธ rails g migration create_join_table_showroom_user showroom user ๋Š” ๋‹ค์Œ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

class CreateJoinTableShowroomUser < ActiveRecord::Migration
  def change
    create_join_table :showrooms, :users do |t|
      # t.references :showroom, index: true, foreign_key: true
      # t.references :user, index: true, foreign_key: true
      # t.index [:showroom_id, :user_id]
      # t.index [:user_id, :showroom_id]
    end
  end
end

+1 @erullmann ์ด๊ฒƒ์€ ๋งค์šฐ ์ข‹์€ ์†”๋ฃจ์…˜์ž…๋‹ˆ๋‹ค. :์Šค๋งˆ์ผ๋ฆฌ:

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰