Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake db:migrate runs into an error for an undefined method

I took over a website that was built by somebody else. I'm now trying to get it up and running on localhost. However, when I migrate it looks like the previous developer put code into the migrations that may rely on a seed already being there. The migrate file looks like this.

def up
  add_column :supplies, :color, :string

  Supply.where(:title => "Shipped").first.update(:color =>'#e20ce8')
end

def down
  remove_column :supplies, :color
end

The error I get on this file when i run rake db:migrate is...

rake aborted!
StandardError: An error has occurred, this and all later migrations 
canceled:

undefined method `update' for nil:NilClass

What can I do to fix this?

like image 443
Steve Avatar asked Feb 17 '26 21:02

Steve


2 Answers

What about doing rake db:schema:load? I believe that would allow you to get going and then allow you to use rake db:migrate going forward.

like image 107
Trinculo Avatar answered Feb 20 '26 11:02

Trinculo


What can be happening is that a previous migration which can be seeding supply model is not running or table is truncated. As a good practice we should not seed data with migrations but rather just build the schema with migrations.

You have 2 options:

How about pulling this code and the other seeders in migrations and putting them in seeds.rb and running rake db:seed

#in seeds.rb
Supply.where(:title => "Shipped").first.update(:color =>'#e20ce8')

Or,

Check before updating the migration.

instance = Supply.where(:title => "Shipped").first
instance.update(color: '#e20ce8') if instance.present?
like image 41
AnkitG Avatar answered Feb 20 '26 09:02

AnkitG