[Ruby on Rails] Add a column with a foreign key constraint

Introduction

environment

Thing you want to do

Creating office utilization matching for inns I want to allow users to bookmark hotels In addition, hotel_favorite has already been modeled.

Method

I want the hotel_favorite model to have user_id and hotel_id. (User_id is already associated)

What i did

rails g migration AddhotelidTohotel_favorites

→ XXXXXXX_add_hotel_ref_tohotel_favorites.rb was created. I edited it as follows.

XXXXXXX_add_hotel_ref_tohotel_favorites.rb


class AddHotelRefTohotelFavorites < ActiveRecord::Migration[6.0]
  def change
    add_reference :hotel_favorites, :hotel, null: false, foreign_key: true
  end
end

here

rails db:migrate

Hotel_id has been added to hotel_favorite as shown below.

schema.rb


  create_table "hotel_favorites", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do |t|
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.bigint "user_id", null: false
    t.bigint "hotel_id", null: false
    t.index ["hotel_id"], name: "index_hotel_favorites_on_hotel_id"
    t.index ["user_id"], name: "index_hotel_favorites_on_user_id"

Summary

From the next time, as in the article below, you should do it at the time of model creation. .. https://qiita.com/hiro266/items/8a0374155cd18adbd7cf

rails g model hotel_favorite user:references hotel:references

Recommended Posts

[Ruby on Rails] Add a column with a foreign key constraint
I want to add a browsing function with ruby on rails
I made a portfolio with Ruby On Rails
[Ruby on Rails] Create a pie chart for each column with Chartkick
[Ruby on Rails] Read try (: [],: key)
[Ruby on Rails] Add / Remove Columns
How to rename a model with foreign key constraints in Rails
Steps to build a Ruby on Rails development environment with Vagrant
[Ruby on Rails] View test with RSpec
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
How to add / remove Ruby on Rails columns
Introducing Rspec with Ruby on Rails x Docker
Publish the app made with ruby on rails
Introducing Rspec, a Ruby on Rails test framework
[Ruby on Rails] A memorandum of layout templates
[Rails] Procedure for linking databases with Ruby On Rails
(Ruby on Rails6) Creating data in a table
Determine the current page with Ruby on Rails
[Ruby on Rails] Upload multiple images with refile
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
[Ruby On Rails] When a model unit test is performed with RSpec using FactoryBot, an error occurs because the foreign key is not entered.
[Introduction] Try to create a Ruby on Rails application
Build a Ruby on Rails development environment on AWS Cloud9
[Ruby on Rails] Delete s3 images with Active Strage
Run Ruby on Rails RSpec tests with GitHub Actions
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
FactoryBot, a solution when played with a foreign key validation
Created RSS / Atom format sitemap with Ruby on Rails
I tried installing Ruby on Rails related plugin with vim-plug
Ruby on rails learning record -2020.10.03
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
Portfolio creation Ruby on Rails
Ruby on rails learning record -2020.10.04
How to use a foreign key with FactoryBot ~ Another solution
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails config configuration
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
A note about the seed function of Ruby on Rails
[Ruby on Rails] Implement login function by add_token_to_users with API
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
How to display a graph in Ruby on Rails (LazyHighChart)
Commentary on partial! --Ruby on Rails
Post a video on rails
[Rails] Add column to devise
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Ruby on rails learning record -2020.10.06