How to write migrationfile

index: It's there to duplicate a specific column in the table to speed up the search.

Create a user_id column with references: t.references: user and index user_id. (No foreign key constraints)

foreign_key: true: Paste a foreign key constraint. Prevents the id of a non-existent User from being registered in user_id. Prevents mistakes that erase records in the parent table.

add_foreign_key (: articles,: users) will add an index if there is no index, and reuse it if there is one. Therefore, the order of adding foreign key constraints → pasting indexes is not acceptable.

rails g model article body: text user: references

migrationfile.rb


class CreateArticles < ActiveRecord::Migration[5.2]
  def change
    create_table :articles do |t|
      t.text :body
      t.references :user, foreign_key: true

      t.timestamps
    end
  end
end

Recommended Posts

How to write migrationfile
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write good code
Bit Tetris (how to write)
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Junit 5 organized
How to write Rails validation
[Ruby] How to write blocks
How to write Rails routing
[Rails] How to write in Japanese
Baseball ball count (how to write)
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
Y-shaped road tour (how to write)
How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
How to deploy
Summary of how to write annotation arguments
[Introduction to Java] How to write a Java program
[Java] How to output and write files!
How to write Spring AOP pointcut specifier
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
How to develop OpenSPIFe
[JavaFX] How to write Eclipse permissions in build.gradle
How to write offline 15th reference question answer
How to use Map
[Rails] How to write when making a subquery
How to use rbenv
Java Development Basics ~ How to Write Programs * Exercise 1 ~
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to write an if statement to improve readability-java
How to use map
How to use collection_select
JUnit 5: How to write test cases in enum
How to adapt Bootstrap
How to use Twitter4J
How to install Docker
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
How to install docker-machine
How to make shaded-jar
How to use identity
How to use hashes
Offline real-time how to write F06 implementation example
How to write code that thinks object-oriented Ruby
How to create docker-compose