[RUBY] Create table and add columns

Add a column to the table

What is a database?

What stores data. For example, like a house. But if you put all the data in your house at once, it's hard to find out what's where.

What is a table?

Storage location for each type of data. At home, it's a room. It is easy to understand and manage if you put it in the relevant place for each type of data. It is called a table.

How to make a table

Name the tables in the database just as there are rooms in the house and each room has a name.

The database interacts with the model to retrieve and store data. It's like a gatekeeper.

Make a model

Files related to the model are created.

   invoke  active_record
      create    db/migrate/20200315054113_create_posts.rb
      create    app/models/post.rb
      invoke    test_unit
      create      test/models/post_test.rb
      create      test/fixtures/posts.yml

Successful generation with create!

The model file is in app / models / post.rb.

Create a table

% Rails g model Multiple files related to the model were created when the model name was given.

Db / migrate / 20200315054113_create_posts.rb (migration file) was created in one of them.

Create the contents of the table with the migration file.

The table was likened to a room. In addition, create types and column names to organize and store the data in it.

If the room is cluttered, it will be difficult to take it out and store it.

Mold

It is easy to understand because it shows what kind of data will be entered.

integer Numerical amount, number of times string Character (short sentence) User name, email address text Character (long sentence) Post text, description datetime Date and time Creation date and time, update date and time

Column name

As the name implies, it is a name. You can decide for yourself.

Now, write the column name and type in the migration file.

class CreatePosts < ActiveRecord::Migration[6.0]
  def change
    create_table :posts do |t|
      t.text :memo
      t.timestamps
    end
  end
end

t. Column type: Enter the column name

I will spend many. Describe the column you want to add.

Now, with the Post table in the database. I will issue a command to create this column in the table.

Reflect the contents written in the migration file.

% Rails db: migrate * Be in the directory of the app you want to create!

This flow is the basis of the database creation flow.

  1. Create a model
  2. Describe the column type and column name in the migration file 3.% rails db:migrate

Recommended Posts

Create table and add columns
How to add columns to a table
[Rails] How to create a table, add a column, and change the column type
Model and table creation
Create table and add columns
How to add columns to a table
[Ruby on Rails] Add / Remove Columns
[Java] Let's create a mod for Minecraft 1.16.1 [Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [9. Add and generate trees]
[Java] Let's create a mod for Minecraft 1.14.4 [8. Add and generate ore]
Create PDF with itext7-Free layout: Table-
[Java] Loop processing and multiplication table
[Ruby on Rails] Add / Remove Columns
Rails model and table naming conventions
Get related table columns with JPA
2. Create Docker image and register Registry
7 Add simple function definitions and calls
Create UITextView with code, add edit end button and prevent keyboard cover