LoginSignup
30
8

More than 1 year has passed since last update.

RailsでDB内を確認したい時

Last updated at Posted at 2022-06-09

はじめに

Rails 6.0.3を使用してローカル環境にてアプリケーションを作成中にアソシエーションを実装しようとしていて自分が作成したDBがどのようなcolumnを設定して、テストで入力している、user情報やuser_id、content内容などを確認して紐付けを行いたいと思う場面がありました。
その際に、ターミナル上で確認した時の内容を備忘録として記事にします。

環境
Ruby: 3.0.1
Rails: 6.0.3

設定内容

テーブルは3つ作成してあり、以下の3つ

  • users
  • blogs
  • favorites

それぞれにカラムを設定
users column

  • name
  • email
  • password_digest

blogs column

  • content
  • user_id

favorites column

  • user_id
  • blog_id

発生していた問題

usersのid(デフォルトで作成されている)とblogsカラムのuser_idを互いに紐づけるため【usersのidとfavoritesのuser_id】と【blogsのUser_idとfavoritesのblog_id】
を互いに紐付けしたかったが、エラー対応をしているうちに現状のそれぞれのDBの状態がわからなってしまったため視覚的に確認したいと思った。

わかったこと

ターミナル上で【rails db】を行うことによりDB内の情報をpsqlの要領で確認できる事がわかった。(ターミナルで作成しているディレクトリに移動後実施)

# rails dbとするとターミナルが`directory_name_development=#に変わる 
XXXXXXX@xxxxxxxx directory_name $ rails db

directory_name_development=#
# この状態で下記のコマンドを実施

\db テーブルスペース表示

directory_name_development=# \db
         List of tablespaces
    Name    |    Owner     | Location 
------------+--------------+----------
 pg_default | xxxxxxxxxxxx | 
 pg_global  | xxxxxxxxxxxx | 
(2 rows)

\d users usersテーブル表示

directory_name_development=# \d users
                                            Table "public.users"
     Column      |              Type              | Collation | Nullable |              Default              
-----------------+--------------------------------+-----------+----------+-----------------------------------
 id              | bigint                         |           | not null | nextval('users_id_seq'::regclass)
 name            | character varying              |           |          | 
 email           | character varying              |           |          | 
 password_digest | character varying              |           |          | 
 created_at      | timestamp(6) without time zone |           | not null | 
 updated_at      | timestamp(6) without time zone |           | not null | 

\d blogs blogsテーブル表示

directory_name_development=# \d blogs
                                          Table "public.blogs"
   Column   |              Type              | Collation | Nullable |              Default              
------------+--------------------------------+-----------+----------+-----------------------------------
 id         | bigint                         |           | not null | nextval('blogs_id_seq'::regclass)
 content    | text                           |           |          | 
 created_at | timestamp(6) without time zone |           | not null | 
 updated_at | timestamp(6) without time zone |           | not null | 
 user_id    | bigint                         |           |          | 

\d favorites favoritesテーブル表示

directory_name_development=# \d favorites
                                          Table "public.favorites"
   Column   |              Type              | Collation | Nullable |                Default                
------------+--------------------------------+-----------+----------+---------------------------------------
 id         | bigint                         |           | not null | nextval('favorites_id_seq'::regclass)
 user_id    | bigint                         |           |          | 
 blog_id    | bigint                         |           |          | 
 created_at | timestamp(6) without time zone |           | not null | 
 updated_at | timestamp(6) without time zone |           | not null | 

select * from blogs; blogsテーブル全て表示

directory_name_development=# select * from blogs;
 id |             content              |         created_at         |         updated_at         | user_id 
----+----------------------------------+----------------------------+----------------------------+---------
 13 | hoge                             | 2022-06-08 05:18:31.53171  | 2022-06-08 05:18:31.53171  |       5
 14 | huga                             | 2022-06-08 05:18:38.037474 | 2022-06-08 05:18:38.037474 |       5
 15 | hello word                       | 2022-06-09 02:38:58.613874 | 2022-06-09 02:38:58.613874 |       5
 16 | I like you                       | 2022-06-09 02:50:40.793617 | 2022-06-09 02:50:40.793617 |       5
 12 | I am taro_xxxxxx                 | 2022-06-08 05:18:25.460709 | 2022-06-09 05:20:03.95975  |       4
(5 rows)

終了時はdirectory_name_development=# \qで抜けます

感想

アプリケーションの作成に慣れていないので、まだ頭の中でテーブルの構成やどの項目を紐づけるなどの感覚が掴めていなかったが、視覚的に確認できることで、頭の整理ができた。

30
8
3

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
30
8