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

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

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

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

Q&A

解決済

1回答

9534閲覧

マイグレーションでundefined method `to_sym' for nil:NilClassと返ってきます。

TakumaN

総合スコア120

Ruby on Rails

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

0グッド

0クリップ

投稿2016/03/25 23:58

編集2016/03/26 00:49

###前提・実現したいこと
bundle exec rake db:migrateでのエラーを改善したいです。

###発生している問題・エラーメッセージ

lenux

1$ bundle exec rake db:migrate 2== 20160325085507 ChangeUserIdToGalleries: migrating ========================== 3-- change_column(:galleries, :user_id, :integer) 4rake aborted! 5StandardError: An error has occurred, this and all later migrations canceled: 6 7undefined method `to_sym' for nil:NilClass 8/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/schema_definitions.rb:258:in `column' 9/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:546:in `block (2 levels) in copy_table' 10/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:539:in `each' 11/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:539:in `block in copy_table' 12/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/schema_statements.rb:216:in `create_table' 13/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:536:in `copy_table' 14/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:529:in `move_table' 15/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:522:in `block in alter_table' 16/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction' 17/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:521:in `alter_table' 18/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/sqlite3_adapter.rb:485:in `change_column' 19/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:665:in `block in method_missing' 20/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:634:in `block in say_with_time' 21/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:634:in `say_with_time' 22/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:654:in `method_missing' 23/Users/takuman/earth/db/migrate/20160325085507_change_user_id_to_galleries.rb:3:in `change' 24/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:608:in `exec_migration' 25/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:592:in `block (2 levels) in migrate' 26/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:591:in `block in migrate' 27/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection' 28/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:590:in `migrate' 29/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:768:in `migrate' 30/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction' 31/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `block in ddl_transaction' 32/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction' 33/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction' 34/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction' 35/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction' 36/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:1044:in `ddl_transaction' 37/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:997:in `execute_migration_in_transaction' 38/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:959:in `block in migrate' 39/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `each' 40/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:955:in `migrate' 41/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:823:in `up' 42/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/migration.rb:801:in `migrate' 43/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:137:in `migrate' 44/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>' 45Tasks: TOP => db:migrate 46(See full trace by running task with --trace)

###migrate/20160325085507_change_user_id_to_galleries.rb

class ChangeUserIdToGalleries < ActiveRecord::Migration def change change_column :galleries, :user_id, :integer end end

###補足情報(言語/FW/ツール等のバージョンなど)
Ruby 2.0.0
rails 4.2.6
rake 11.1.1

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

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

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

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

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

raccy

2016/03/26 00:30

migrate/20160325085507_change_user_id_to_galleries.rb があやしいですので、それも一緒に貼り付けていただけませんか?
TakumaN

2016/03/26 01:13

追加しました!
guest

回答1

0

ベストアンサー

考えられる原因として、galleriesテーブルにuser_idカラムがないと推測されます。change_columnは既存のカラムの変更になりますが、user_idはすでに作成されている状態でしょうか?galleriesテーブルに関する各migrationを確認し、user_idが作られるかどうか確認してみて下さい。また、migrateする既存DBでuser_idがあるかも確認してみて下さい。developmentのDBであれば、rake db:resetや``rake db:migrate:reset`でDBをはじめから作り直した場合どうなるかを確認してみて下さい。

投稿2016/03/26 01:40

raccy

総合スコア21735

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

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

TakumaN

2016/03/26 02:29

user_idはすでに作成されており、マイグレーションファイルもあります。 sqlite3のデータベースから確認してみてもgalleriesテーブル内にuser_idがちゃんとありました。 rake db:resetを実行するとgalleriesテーブルだけがデータベースから消えました。(他のテーブルはちゃんと残っています。) そのあとにrake db:migrate:resetを実行すると、またgalleriesテーブルが出てきました。 そのあとに再びrake db:migrateでマイグレーションを実行してみても同じエラーが表示されました。
TakumaN

2016/03/26 11:19

新たにマイグレーションファイルを生成して、rake db:migrateするのではなく、 直接、変更したいカラムのマイグレーションファイルを書き直してから、rake db:migrate:resetするというやり方にしたところ、ちゃんとデータベースに反映されました! ご丁寧なアドバイスを何度もしてくださって、どうもありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問