#238 feat: user_ips の id を削除(#231)

Merged
みてるぞ merged 1 commits from feature/231 into main 2 weeks ago
  1. +2
    -0
      backend/app/models/user_ip.rb
  2. +47
    -0
      backend/db/migrate/20260127000900_change_user_ips_to_composite_pk.rb
  3. +2
    -3
      backend/db/schema.rb

+ 2
- 0
backend/app/models/user_ip.rb View File

@@ -1,4 +1,6 @@
class UserIp < ApplicationRecord class UserIp < ApplicationRecord
self.primary_key = :user_id, :ip_address_id

belongs_to :user belongs_to :user
belongs_to :ip_address belongs_to :ip_address




+ 47
- 0
backend/db/migrate/20260127000900_change_user_ips_to_composite_pk.rb View File

@@ -0,0 +1,47 @@
class ChangeUserIpsToCompositePk < ActiveRecord::Migration[8.0]
def up
execute <<~SQL
ALTER TABLE
user_ips
MODIFY COLUMN id BIGINT NOT NULL
;
SQL

execute <<~SQL
ALTER TABLE
user_ips
DROP PRIMARY KEY
;
SQL

remove_column :user_ips, :id

execute <<~SQL
ALTER TABLE
user_ips
ADD PRIMARY KEY (user_id, ip_address_id)
;
SQL

remove_index :user_ips, name: 'index_user_ips_on_user_id'
end

def down
execute <<~SQL
ALTER TABLE
user_ips
DROP PRIMARY KEY
;
SQL

execute <<~SQL
ALTER TABLE
user_ips
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
, ADD PRIMARY KEY (id)
;
SQL

add_index :user_ips, :user_id, name: 'index_user_ips_on_user_id'
end
end

+ 2
- 3
backend/db/schema.rb View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.


ActiveRecord::Schema[8.0].define(version: 2026_01_26_124100) do
ActiveRecord::Schema[8.0].define(version: 2026_01_27_000900) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false t.string "name", null: false
t.string "record_type", null: false t.string "record_type", null: false
@@ -144,13 +144,12 @@ ActiveRecord::Schema[8.0].define(version: 2026_01_26_124100) do
t.index ["tag_name_id"], name: "index_tags_on_tag_name_id", unique: true t.index ["tag_name_id"], name: "index_tags_on_tag_name_id", unique: true
end end


create_table "user_ips", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "user_ips", primary_key: ["user_id", "ip_address_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id", null: false t.bigint "user_id", null: false
t.bigint "ip_address_id", null: false t.bigint "ip_address_id", null: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["ip_address_id"], name: "index_user_ips_on_ip_address_id" t.index ["ip_address_id"], name: "index_user_ips_on_ip_address_id"
t.index ["user_id"], name: "index_user_ips_on_user_id"
end end


create_table "user_post_views", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| create_table "user_post_views", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|


Loading…
Cancel
Save