実現したいこと
Railsにて投稿フォームを作成し,タグ付けができる機能を実装したいと思っています。
その上で,Tagモデルと中間テーブルとしてpostTagテーブルを作成し,マイグレーションをしたいです。
発生している問題・分からないこと
すでに同じ名前のテーブル(tags)が存在していると出てきてしまい,マイグレーションができません。
解決策を何卒ご教授いただきたいです。
エラーメッセージ
error
1rails db:migrate 2== 20240206113403 CreateTags: migrating ======================================= 3-- create_table(:tags) 4rails aborted! 5StandardError: An error has occurred, this and all later migrations canceled: 6 7PG::DuplicateTable: ERROR: relation "tags" already exists 8/app/db/migrate/20240206113403_create_tags.rb:3:in `change' 9/app/bin/rails:5:in `<top (required)>' 10/app/bin/spring:10:in `block in <top (required)>' 11/app/bin/spring:7:in `<top (required)>' 12 13Caused by: 14ActiveRecord::StatementInvalid: PG::DuplicateTable: ERROR: relation "tags" already exists 15/app/db/migrate/20240206113403_create_tags.rb:3:in `change' 16/app/bin/rails:5:in `<top (required)>' 17/app/bin/spring:10:in `block in <top (required)>' 18/app/bin/spring:7:in `<top (required)>' 19 20Caused by: 21PG::DuplicateTable: ERROR: relation "tags" already exists 22/app/db/migrate/20240206113403_create_tags.rb:3:in `change' 23/app/bin/rails:5:in `<top (required)>' 24/app/bin/spring:10:in `block in <top (required)>' 25/app/bin/spring:7:in `<top (required)>' 26Tasks: TOP => db:migrate 27(See full trace by running task with --trace)
ブラウザ側
StandardError An error has occurred, this and all later migrations canceled: PG::DuplicateTable: ERROR: relation "tags" already exists Extracted source (around line #113): 111 112 113 114 115 116 start = Process.clock_gettime(Process::CLOCK_MONOTONIC) result = exec_without_profiling(*args, &blk) elapsed_time = SqlPatches.elapsed_time(start) record = ::Rack::MiniProfiler.record_sql(args[0], elapsed_time) result.instance_variable_set("@miniprofiler_sql_id", record) if result Rails.root: /app
該当のソースコード
db/migrate/20240206113552_create_post_tags.rb
1class CreatePostTags < ActiveRecord::Migration[6.1] 2 def change 3 create_table :post_tags do |t| 4 t.references :post, null: false, foreign_key: true 5 t.references :tag, null: false, foreign_key: true 6 t.timestamps 7 end 8 add_index :post_tags, [:post_id,:tag_id],unique: true 9 end 10end 11
db/migrate/20240206113403_create_tags.rb
1class CreateTags < ActiveRecord::Migration[6.1] 2 def change 3 create_table :tags do |t| 4 t.string :name, null: false 5 6 t.timestamps 7 end 8 add_index :tags, :name, unique:true 9 end 10end 11
Status Migration ID Migration Name -------------------------------------------------- up 20210707021252 Create users up 20210707021454 Create profiles up 20210707041601 Create posts up 20210709035845 Create comments down 20240206113403 Create tags down 20240206113552 Create post tags
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
データベースを確認し,tagsテーブルがないことが確認できました。
SELECT * FROM tags; id | name | created_at | updated_at ----+------+------------+------------ (0 rows)
コンソールでも確認
irb(main):001:0> Tag.count (1.9ms) SELECT COUNT(*) FROM "tags" => 0
ここからどうしたらいいかわからず...完全に止まってしまいました。
補足
特になし
回答1件
あなたの回答
tips
プレビュー