[Rails / MySQL] Database logical name setting

About setting the database logical name

This time (explained using the contacts table as an example)

table of contents

  1. About physical and logical names
  2. In setting the logical name
  3. Setting the logical name

1. About physical and logical names

Physical name

The actual name your computer knows Example: [contacts] table

Logical name

Name (name) set so that it can be recognized by humans Example: [Inquiry] table

2. In setting the logical name

In large-scale project development, there are situations where the number of tables is 100 or more, or there are dozens of columns in one table. With so many, finding tables and columns and explaining other members can be a daunting task. If you don't understand the meaning of English with only English notation (** physical name ), you have to search. It's a little easier to have Japanese notation ( logical name **) ... (It was easy to set!)

3. Setting the logical name

Let's take the contacts table as an example. Create a migration file with the following command.

rails g model contact

Describe the table and column information in the created migration file.

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

If it is reflected in the migration file database as it is, the logical name cannot be set.

** If no logical name is set ** before.png

Set the logical name in the migration file and describe the table and column information.

db/migrate/xxx_create_contacts.rb


class CreateContacts < ActiveRecord::Migration[5.0]
  def change
    create_table :contacts, comment: 'Contact Us' do |t|
      t.string :name_sei,      null: false, default: '', comment: 'Surname'
      t.string :name_mei,      null: false, default: '', comment: 'Name'
      t.string :name_sei_kana, null: false, default: '', comment: 'Last name Kana'
      t.string :name_mei_kana, null: false, default: '', comment: 'Name Kana'
      t.string :sex,           null: false, default: '', comment: 'sex'
      t.string :age,           null: false, default: '', comment: 'age'
      t.string :email,         null: false, default: '', comment: 'mail address'
      t.string :introducer,    null: false, default: '', comment: 'introducer'
      t.text   :content                                , comment: 'Contents'
      t.timestamps
    end
  end
end

Execute the following command to reflect the migration file.

rails db:migrate

Then, the logical name is set in the comment part as shown below.

** When a logical name is set **

after.png

By setting a logical name, you can efficiently browse the database and share it with team members. This is the setting of the database logical name.

Recommended Posts

[Rails / MySQL] Database logical name setting
Rails database basics
[Rails] Development with MySQL
[Ruby on Rails] From MySQL construction to database change
After setting database to mysql, bundle install causes an error
Rails6 (MySQL, Ubuntu environment, Cloud9)
[Rails / MySQL] Mac environment construction