LoginSignup
0
0

More than 1 year has passed since last update.

Mysql2::Error: Unknown column 'messages.room_id' in 'where clauseの解決法

Last updated at Posted at 2022-05-21

開発環境

Mac
Ruby 2.6.5
Rails 6.1.3.2

エラーメッセージ

$ Mysql2::Error: Unknown column 'messages.room_id' in 'where clause
rooms/show.html.erb
<% if @messages.present? %> <=この部分がエラー
<% @messages.each do |m| %>
<strong><%= m.content %></strong>
<small>by <strong><a href="/users/<%= m.user_id %>"><%= m.user.email %>さん</a></strong></small>
<hr>
<% end %>
<%else %>
<h3 class="text-center">メッセージはまだありません</h3>
<% end %>
class CreateMessages < ActiveRecord::Migration[6.1]
  def change
    create_table :messages do |t|
     
      t.text :content
      t.timestamps
    end
  end
end

解決方法

% rails destroy model message  
% rails db 

この後

 mysql> SHOW TABLES;
+-----------------------------------+
| Tables_in_アプリ名_development |
+-----------------------------------+
| active_storage_attachments        |
| active_storage_blobs              |
| active_storage_variant_records    |
| ar_internal_metadata              |
| comments                          |
| entries                           |
| likes                             |
| messages   <=確認                  |
| posts                             |
| relationships                     |
| rooms                             |
| schema_migrations                 |
| tag_maps                          |
| tags                              |
| users                             |
+-----------------------------------+
mysql> drop table messages;
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW TABLES;
+-----------------------------------+
| Tables_in_アプリ名_development |
+-----------------------------------+
| active_storage_attachments        |
| active_storage_blobs              |
| active_storage_variant_records    |
| ar_internal_metadata              |
| comments                          |
| entries                           |
| likes                             |
| posts                             |
| relationships                     |
| rooms                             |
| schema_migrations                 |
| tag_maps                          |
| tags                              |
| users                             |
+-----------------------------------+
14 rows in set (0.00 sec)
mysql> exit
Bye
$ rails g model message user:references room:references content:text
 % rails db:migrate
Running via Spring preloader in process 16914
== 20220521062317 CreateMessages: migrating ===================================
-- create_table(:messages)
   -> 0.0164s
== 20220521062317 CreateMessages: migrated (0.0164s) ==========================

が出ればOKです。

エラーの原因

room_idカラムがなかったからです。
このエラーに対しては外部キー制約付きカラム(room_idカラム)を追加する必要がありました。

参考記事

デプロイ後のMysql2::Error: Unknown column エラー
Mysql2::Error: Table '' already existsと表示されてしまった時の対処法
Mysql2::Error: Unknown column 'favorites.true' in 'where clause'の対処について
RailsでややこしいDM機能を1万字でくわしく解説してみた

0
0
0

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
0
0