20

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?

huertanix
  • 761
  • 1
  • 9
  • 22

2 Answers2

20

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.

Josh W Lewis
  • 1,319
  • 11
  • 19
  • 1
    Yup, it was a dev DB with only one record in it; Changing the migrations to create an "event_id" column and use an integer data type and recreating the DB did the trick. – huertanix Jul 29 '13 at 01:09
  • How would you drop the database and recreate the DB? – camdixon Apr 12 '14 at 15:03
  • 9
    `bundle exec rake db:reset` is usually effective, but when your schema.rb is wrong, something like `bundle exec rake db:drop db:create db:migrate` works better. – Josh W Lewis Apr 13 '14 at 15:58
0

Just in case this helps someone. I was doing this on a sqlite dev db, and as mentioned above, it was likely in a wonky state from all my experimental migrations. By deleting the sqlite file, recreating and running all the migrations, it is now fine.

Jay
  • 594
  • 10
  • 23