前提・実現したいこと
Ruby on Railsで場所を登録するようなシステムを作っています。
rails g model 'モデル名' (今回はspaceとcomment)でモデルを作成後、
db/xxxxxxxx create_モデル名にて記載、モデルにバリデーションの追加を行った後にマイグレーションを行いたい
発生している問題・エラーメッセージ ターミナル上でrails db:migrate後
== 20210813080601 CreateComments: migrating =================================== -- create_table(:comments) rails aborted! StandardError: An error has occurred, all later migrations canceled: Column `user_id` on table `comments` does not match column `id` on `users`, which has type `bigint(20)`. To resolve this issue, change the type of the `user_id` column on `comments` to be :bigint. (For example `t.bigint :user_id`). Original message: Mysql2::Error: Cannot add foreign key constraint /Users/nakadaira/projects/spacemolkky/db/migrate/20210813080601_create_comments.rb:3:in `change' /Users/nakadaira/projects/spacemolkky/bin/rails:9:in `<top (required)>' /Users/nakadaira/projects/spacemolkky/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: ActiveRecord::MismatchedForeignKey: Column `user_id` on table `comments` does not match column `id` on `users`, which has type `bigint(20)`. To resolve this issue, change the type of the `user_id` column on `comments` to be :bigint. (For example `t.bigint :user_id`). Original message: Mysql2::Error: Cannot add foreign key constraint /Users/nakadaira/projects/spacemolkky/db/migrate/20210813080601_create_comments.rb:3:in `change' /Users/nakadaira/projects/spacemolkky/bin/rails:9:in `<top (required)>' /Users/nakadaira/projects/spacemolkky/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>' Caused by: Mysql2::Error: Cannot add foreign key constraint /Users/nakadaira/projects/spacemolkky/db/migrate/20210813080601_create_comments.rb:3:in `change' /Users/nakadaira/projects/spacemolkky/bin/rails:9:in `<top (required)>' /Users/nakadaira/projects/spacemolkky/bin/spring:15:in `<top (required)>' bin/rails:3:in `load' bin/rails:3:in `<main>'
該当のソースコード
Ruby
120210813080601_create_comments.rb 2 3class CreateComments < ActiveRecord::Migration[6.0] 4 def change 5 create_table :comments do |t| 6 t.text :text, null: false 7 t.references :user, null: false, foreign_key: true 8 t.references :space, null: false, foreign_key: true 9 t.timestamps 10 end 11 end 12end
Ruby
120210813080602_create_spaces.rb 2 3class CreateSpaces < ActiveRecord::Migration[6.0] 4 def change 5 create_table :spaces do |t| 6 t.string :space_name , null: false 7 t.datetime :opening_at 8 t.integer :price 9 t.integer :prefecture_id , null: false 10 t.string :city , null: false 11 t.string :block , null: false 12 t.integer :ground_id , null: false 13 t.text :explanation , null: false 14 t.references :comment , null: false, foreign_key: true 15 t.references :user , null: false, foreign_key: true 16 t.timestamps 17 end 18 end 19end 20
Ruby
1model/comments.rb 2 3class Comment < ApplicationRecord 4 belongs_to :user 5 belongs_to :space 6end
Ruby
1model/spaces.rb 2 3class Space < ApplicationRecord 4 has_many_attached :images 5 belongs_to :user 6 has_many :comments 7end
Ruby
1schema.rb 2 3# This file is auto-generated from the current state of the database. Instead 4# of editing this file, please use the migrations feature of Active Record to 5# incrementally modify your database, and then regenerate this schema definition. 6# 7# This file is the source Rails uses to define your schema when running `rails 8# db:schema:load`. When creating a new database, `rails db:schema:load` tends to 9# be faster and is potentially less error prone than running all of your 10# migrations from scratch. Old migrations may fail to apply correctly if those 11# migrations use external dependencies or application code. 12# 13# It's strongly recommended that you check this file into your version control system. 14 15ActiveRecord::Schema.define(version: 0) do 16 17end
試したこと
dbの日付を変更してみたり、一回全てrollbackしてみたりを繰り返してます