LoginSignup
0
0

More than 3 years have passed since last update.

【Rails・MySQL】データベース論理名の設定について

Posted at

データベース論理名の設定について

今回は(contacts[お問い合わせ]テーブルを例に説明)

目次

  1. 物理名・論理名について
  2. 論理名を設定するにあたって
  3. 論理名の設定

1. 物理名・論理名について

物理名

コンピュータが認識している実際の名前
例:[contacts]テーブル

論理名

人間が認識できるように設定した名前(呼び名)
例:[お問い合わせ]テーブル

2. 論理名を設定するにあたって

大規模のプロジェクト開発では、テーブル数が100以上となったり、1テーブルにカラム数が数十個存在するという状況があります。これだけ数が多いとテーブル・カラムを探したり、他のメンバーについて説明を行うことが非常に手間となったりします。英語表記(物理名)だけであると、英語の意味を理解できていない場合、検索をしなければなりません。
日本語表記(論理名)があると少し楽なんだけど...(簡単に設定できました!)

3. 論理名の設定

contacts[お問い合わせ]テーブルを例に説明をしていきます。
以下コマンドでマイグレーションファイルを作成します。

rails g model contact

作成されたマイグレーションファイルにテーブル・カラム情報を記載していきます。

db/migrate/xxx_create_contacts.rb
class CreateContacts < ActiveRecord::Migration[5.0]
  def change
    create_table :contacts do |t|
      t.string :name_sei,      null: false, default: ''
      t.string :name_mei,      null: false, default: ''
      t.string :name_sei_kana, null: false, default: ''
      t.string :name_mei_kana, null: false, default: ''
      t.string :sex,           null: false, default: ''
      t.string :age,           null: false, default: ''
      t.string :email,         null: false, default: ''
      t.string :introducer,    null: false, default: ''
      t.text   :content
      t.timestamps
    end
  end
end

このままマイグレーションファイルデータベースに反映させてしまうと論理名を設定することができません。

論理名を設定していない場合
before.png

マイグレーションファイルに論理名を設定し、テーブル・カラム情報を記載します。

db/migrate/xxx_create_contacts.rb
class CreateContacts < ActiveRecord::Migration[5.0]
  def change
    create_table :contacts, comment: 'お問い合わせ' do |t|
      t.string :name_sei,      null: false, default: '', comment: '姓'
      t.string :name_mei,      null: false, default: '', comment: '名'
      t.string :name_sei_kana, null: false, default: '', comment: '姓カナ'
      t.string :name_mei_kana, null: false, default: '', comment: '名カナ'
      t.string :sex,           null: false, default: '', comment: '性別'
      t.string :age,           null: false, default: '', comment: '年齢'
      t.string :email,         null: false, default: '', comment: 'メールアドレス'
      t.string :introducer,    null: false, default: '', comment: '紹介者'
      t.text   :content                                , comment: '内容'
      t.timestamps
    end
  end
end

以下コマンドを実行し、マイグレーションファイルを反映させます。

rails db:migrate

すると以下のようにコメント部分に論理名が設定されます。

論理名を設定した場合

after.png

論理名を設定することにより、データベースの閲覧・チームメンバーとの共有を効率的にすることができます。
以上でデータベース論理名の設定についてになります。

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