Rails 4: schema.db shows "Could not dump table "events" because of following NoMethodError# undefined method `[]' for nil:NilClass"

rails-migrations, ruby-on-rails, ruby-on-rails-4

Solution

I think your last migration is wrong. I would change it to this:

class AddEventRefToPressBlurbs < ActiveRecord::Migration
  def change
    add_reference :press_blurb, :event
  end
end

Unfortunately, your database is likely in a wonky state because it has an invalid column type (i.e. there is no 'reference' column type, but sqlite made it anyway). Hopefully, it's just a development database so you can just delete it and start fresh.

Problem

I've been working on a Rails 4.0 application with sqlite (default for Rails development environment) for events (hackathons) which has a parent model, Event, for which there can be many Press_Blurbs. First I ran some scaffolding generators which created some migrations that I ran seemingly without issue: ``` class CreateEvents < ActiveRecord::Migration def change create_table :events do |t| t.string :city t.string :theme t.datetime :hackathon_start t.datetime :hackathon_end t.datetime :show_start t.datetime :show_end t.text :about t.string :hack_rsvp_url t.string :show_rsvp_url t.timestamps end end end class CreatePressBlurbs < ActiveRecord::Migration def change create_table :press_blurbs do |t| t.string :headline t.string :source_name t.string :source_url t.string :logo_uri t.timestamps end end end ``` Then I added some relationships to the models: ``` class Event < ActiveRecord::Base has_many :press_blurbs end class PressBlurb < ActiveRecord::Base belongs_to :event end ``` ...and added/ran a migration to add a table reference: ``` class AddEventRefToPressBlurbs < ActiveRecord::Migration def change add_column :press_blurbs, :event, :reference end end ``` Nevertheless, when I look at schema.db this is what I see instead of tables definitions: ``` # Could not dump table "events" because of following NoMethodError # undefined method `[]' for nil:NilClass # Could not dump table "press_blurbs" because of following NoMethodError # undefined method `[]' for nil:NilClass ``` Other unrelated tables show up in schema.rb perfectly fine, but these do not. Any idea what's going on?

Original source