LoginSignup
0
0

データ型 references型について

Last updated at Posted at 2023-08-08

references型について

references型はRailsのマイグレーションでよく使われるデータ型で、関連するモデル間で「参照」を表現するためのものです。一般に、あるテーブルのレコードが別のテーブルのレコードを指し示す必要がある場合に使用されます。

referencesを使うと、自動的に整数型のカラムを作成し、そのカラムに対する外部キー制約を設定します。

以下がマイグレーションコードの例です。

create_table :comments do |t|
  t.references :post, null: false, foreign_key: true
  # 他のカラム
  t.timestamps
end

このコードは、post_idという名前の整数型カラムをcommentsテーブルに追加します。そして、このカラムはpostsテーブルのidカラムへの外部キーとして機能します。foreign_key: trueの部分が、外部キー制約を追加するための指定です。

したがって、t.references :postは以下の2行と同じ意味になります。

t.integer :post_id, null: false
add_foreign_key :comments, :posts, column: :post_id

referencesを使用することで、コードがシンプルになり、関連するテーブル間の関係が明確に表現されます。


コメントテーブルの例

user,postテーブルがある前提です。

スクリーンショット 2023-08-08 13.33.49.png
commentsテーブルを作成するために、上記のカラムを持つマイグレーションファイルを作成する場合は、以下のように書くことができます。

class CreateComments < ActiveRecord::Migration[6.1]
  def change
    create_table :comments do |t|
      t.references :user, null: false, foreign_key: true
      t.references :post, null: false, foreign_key: true
      t.text :content
      t.timestamps
    end
  end
end

このコードでは、referencesを使用してuser_idとpost_idの2つの整数型カラムを作成し、それぞれusersテーブルとpostsテーブルへの外部キー制約を追加しています。

こうすることで、コメントは特定のユーザーと投稿に関連付けられ、データベースの整合性が保たれます


relationshipsテーブル(フォロー、フォロワー)の例

class CreateRelationships < ActiveRecord::Migration[6.1]
  def change
    create_table :relationships do |t|
      t.references :follower, references: :users, null: false
      t.references :followed, references: :users, null: false
      t.timestamps
    end

    add_foreign_key :relationships, :users, column: :follower_id
    add_foreign_key :relationships, :users, column: :followed_id
  end
end

relationshipsテーブルは、ユーザー間のフォロー関係を表すテーブルです。follower_idとfollowed_idカラムは、共にusersテーブルを参照するための外部キーとなります。

add_foreign_key :relationships, :users, column: :follower_id
add_foreign_key :relationships, :users, column: :followed_id

ここではrelationshipsテーブルに2つの外部キー制約を追加しています。

follower_idカラム: このカラムはusersテーブルのidカラムを参照します。つまり、relationshipsテーブルのfollower_idカラムに格納される値は、usersテーブルのidカラムの値でなければなりません。フォローしているユーザーのIDを格納します。

followed_idカラム: このカラムもusersテーブルのidカラムを参照します。フォローされているユーザーのIDを格納します。

外部キー制約により、relationshipsテーブルにはusersテーブルに存在するユーザーIDのみが格納されるようになります。この制約がないと、存在しないユーザーIDがfollower_idやfollowed_idに格納される可能性があります。

t.references :follower, references: :users, null: false
t.references :followed, references: :users, null: false

:snowflake:t.references :follower, references: :users, null: false
followerはこのテーブルのカラム名で、usersテーブルを参照します。
null: falseはこのカラムにNULL値が入らないように制約をかけています。つまり、follower_idが空であることを許可しないという意味です。

:snowflake:t.references :followed, references: :users, null: false
followedもこのテーブルのカラム名で、同様にusersテーブルを参照します。
null: false制約がこちらにも適用されています。

このコードにより、relationshipsテーブルにはfollower_idとfollowed_idというカラムが追加され、それぞれがusersテーブルのidカラムを参照します。


scheme.rbには以下のように表示されます。

scheme.rb
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
  add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
  add_foreign_key "bookmarks", "posts"
  add_foreign_key "bookmarks", "users"
  add_foreign_key "comments", "posts"
  add_foreign_key "comments", "users"
  add_foreign_key "ingredients", "posts"
  add_foreign_key "likes", "posts"
  add_foreign_key "likes", "users"
  add_foreign_key "posts", "users"
  add_foreign_key "relationships", "users", column: "followed_id"
  add_foreign_key "relationships", "users", column: "follower_id"

参考

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