feat: user_post_views から id を削除(#230) #239

Merged
みてるぞ merged 1 commits from feature/230 into main 2026-01-27 01:04:17 +09:00
3 changed files with 51 additions and 3 deletions
+2
View File
@@ -1,4 +1,6 @@
class UserPostView < ApplicationRecord
self.primary_key = :user_id, :post_id
belongs_to :user
belongs_to :post
@@ -0,0 +1,47 @@
class ChangeUserPostViewsToCompositePk < ActiveRecord::Migration[8.0]
def up
execute <<~SQL
ALTER TABLE
user_post_views
MODIFY COLUMN id BIGINT NOT NULL
;
SQL
execute <<~SQL
ALTER TABLE
user_post_views
DROP PRIMARY KEY
;
SQL
remove_column :user_post_views, :id
execute <<~SQL
ALTER TABLE
user_post_views
ADD PRIMARY KEY (user_id, post_id)
;
SQL
remove_index :user_post_views, name: 'index_user_post_views_on_user_id'
end
def down
execute <<~SQL
ALTER TABLE
user_post_views
DROP PRIMARY KEY
;
SQL
execute <<~SQL
ALTER TABLE
user_post_views
ADD COLUMN id BIGINT NOT NULL AUTO_INCREMENT FIRST
, ADD PRIMARY KEY (id)
;
SQL
add_index :user_post_views, :user_id, name: 'index_user_post_views_on_user_id'
end
end
+2 -3
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2026_01_27_000900) do
ActiveRecord::Schema[8.0].define(version: 2026_01_27_005300) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -152,13 +152,12 @@ ActiveRecord::Schema[8.0].define(version: 2026_01_27_000900) do
t.index ["ip_address_id"], name: "index_user_ips_on_ip_address_id"
end
create_table "user_post_views", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
create_table "user_post_views", primary_key: ["user_id", "post_id"], charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "post_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["post_id"], name: "index_user_post_views_on_post_id"
t.index ["user_id"], name: "index_user_post_views_on_user_id"
end
create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|