21
15

More than 3 years have passed since last update.

【Rails DBエラー】SQLite3::SQLException: table "✖︎✖︎✖︎" already existsが出るときの解決

Posted at

【Rails DBエラー】SQLite3::SQLException: table "✖︎✖︎✖︎" already existsが出るときの解決

なぜこのエラーが出るか

このエラーはすでに作成されているテーブルが再度作成されているときに出現するエラーです。
モデルを作った後に、カラム名とかを間違えて「rails db:rollback」とかz「rails destroy モデル名」とかして、また「rails db:migrate」をした時におきますね。
↓こんなエラー

terminal
> rails db:migrate
== 20201111115213 CreateComments: migrating ===================================
-- create_table(:comments)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

Caused by:
SQLite3::SQLException: table "✖︎✖︎✖︎✖︎✖︎✖︎" already exists

どう解決するか

いたってシンプルで、すでに作られているテーブルを一回削除すれば良いです。
なので、下で紹介する方法は、「SQLiteにて既存のテーブルを削除する方法」になります。

terminal
> rails db

sqlite> .tables
table-example1 table-example2 table-example3 table-example4 

sqlite> drop table 既存のテーブルの名前;
sqlite> .quit

# この後、またちゃんとマイグレーションすれば好きです
> rails db:migrate
# これであなたのエラー解決できれば嬉しいです!

追記/RailsでのSQlite、Mysqlの操作コマンド集

SQLiteへの接続

terminal
sqlite> rails db

SQLiteの接続を切る

terminal
sqlite> .quit
それか
Ctrl+D

テーブル一覧

terminal
sqlite> .table

Mysqlへの接続

terminal
Myslq> mysql -u root -p PASSWORD;

Mysqlデータベースの確認

terminal
Myslq> show databeses;

Mysqlテーブルの確認

terminal
Myslq> show tables;
21
15
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
15