質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

3回答

1440閲覧

rails g db:migrate ができない

nakadaira

総合スコア0

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/08/13 09:44

編集2021/08/16 00:57

前提・実現したいこと

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してみたりを繰り返してます

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

HTMLdoc

2021/08/13 13:19

schema.rbを見せてください。
kameoman

2021/08/13 13:56

お疲れ様です。 ファイル名の時系列(読み込む順番)が原因と私は考えます。 ただ'HTMLdoc'さんもおっしゃる通り、schema.rbでデータベース全体を確認したいです。
nakadaira

2021/08/16 00:54

schema.rbの情報を編集しました。お手数ですがよろしくお願いします
guest

回答3

0

HTMLdoc さんの指摘があたってるのですが、migration作成の順番も問題です。
CreateComments で

t.references :user, null: false, foreign_key: true t.references :space, null: false, foreign_key: true

してますから、commentの前に user と space を作っておく必要があります。
ただ、、、、
CreateSpaces で
t.references :comment , null: false, foreign_key: true
してるから、spaceより先にcommentを作っておく必要があります。
困りましたね。

モデルの関連の付け方がおかしいです。
commentとspaceの関連つけを片方向にしてください。

投稿2021/08/21 09:27

winterboum

総合スコア23347

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

t.references :user, null: false, foreign_key: true
としていますが、
usersテーブルは存在しないためuserを参照することはできませんってことですね。
usersテーブルがそもそもありません。

schema.rbを見ても空白ですし。

投稿2021/08/21 06:19

HTMLdoc

総合スコア67

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

マイグレーションファイルにコンマつけ忘れでは?
(textのnull falseのあと)

↑これ勘違いでした。すみません

投稿2021/08/14 00:03

編集2021/08/14 04:39
kokitail

総合スコア135

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問