# Rake db:schema:load insists on querying my database for data I haven't loaded

**URL:** <https://www.ruby-forum.com/t/rake-dbload-insists-on-querying-my-database-for-data-i-havent-loaded/173541>\
**Category:** Rails\
**Created:** [August 11, 2009, 6:32am UTC](https://www.ruby-forum.com/t/rake-dbload-insists-on-querying-my-database-for-data-i-havent-loaded/173541 "2009-08-11T06:32:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [August 11, 2009, 6:32am UTC](https://www.ruby-forum.com/t/rake-dbload-insists-on-querying-my-database-for-data-i-havent-loaded/173541/1 "2009-08-11T06:32:46Z")

</div>

Rails 2.3.2. Ruby 1.8.7. MySQL 5.x

I have used “rake db:schema:dump” to generate a migration structure to  
go from development to production. I want to use “rake  
db:schema:load” to load it into my new database. Sounds like a plan.

When I run “rake db:schema:load” on the new database (development or  
production), I get the following error:

rake aborted! Mysql::Error: Table 'mojo\_development.statuses' doesn't exist: SELECT \* FROM `statuses` WHERE (`statuses`.`name` = 'New') LIMIT 1

(production gives the same, but database name is mojo\_production).

The Select is not in the schema. If I trace it back, I find the  
Select is generated from the following method in my Mojo model:

def self.new\_state  
for\_name(‘New’).first  
end

where for\_name is a :named\_scope generating the select statement  
above.

So my questions are:

1. WHY does db:schema:load run queries against my database when the  
data (and tables) aren’t there?

and

1. HOW do I fix this so it won’t happen again?

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [August 11, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/rake-dbload-insists-on-querying-my-database-for-data-i-havent-loaded/173541/2 "2009-08-11T14:55:00Z")

</div>

Posting the results of running the rake command with --trace always  
helps, but I’d say the likely culprit here is something in your  
environment / initializers. Does that function get called from  
environment.rb or an initializer?

–Matt J.

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [August 11, 2009, 9:51pm UTC](https://www.ruby-forum.com/t/rake-dbload-insists-on-querying-my-database-for-data-i-havent-loaded/173541/3 "2009-08-11T21:51:08Z")

</div>

Eric Smith wrote:

> Rails 2.3.2. Ruby 1.8.7. MySQL 5.x
> 
> I have used “rake db:schema:dump” to generate a migration structure to  
> go from development to production. I want to use “rake  
> db:schema:load” to load it into my new database. Sounds like a plan.
> 
> When I run “rake db:schema:load” on the new database (development or  
> production), I get the following error:

Your rake task is picking up your development environment from  
config/environment.rb.

Here’s what you can do to start off with a clean production schema  
provided you are the only one working on your db and you have  
foolishly/wisely modified your db schema without going through a proper  
migration strategy…

1. perform a rake db:schema:dump
2. remove all prior migration files
3. perform a rake db:migrate
4. open the migration file just created, and cut and paste the contents  
from the schema.rb file that you created in step 1. (during the paste,  
just copy the relevant parts, avoid the database create part…)
5. Now you can use that migration file on production
6. Ask that girl in finance out on a date like you promised yourself 2  
weeks ago.

hth

ilan

---

<div class="post-metadata">

**Author:** ![soderic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@soderic](https://www.ruby-forum.com/u/soderic)\
**Post date:** [August 11, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/rake-dbload-insists-on-querying-my-database-for-data-i-havent-loaded/173541/4 "2009-08-11T22:05:21Z")

</div>

On Aug 11, 5:31 am, Polydectes [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. WHY does db:schema:load run queries against my database when the  
> data (and tables) aren’t there?
> 
> and
> 
> 1. HOW do I fix this so it won’t happen again?

rake db:schema:load requires the environment task (which loads your  
app config and so on)  
in production config.cache\_classes is true, which means that your  
applications models are loaded  
your Mojo model is loaded, and reaches the line

named\_scope :some\_scope, :conditions =\> {… self.new\_state }

which runs your new\_state ,method, which queries the database

One possible solution: alter your environment so that cache classes  
doesn’t get set to true in cases like this. I outlined a few ways of  
doing this at

> **[When cache\_classes gets you down - Space Vatican](https://www.spacevatican.org/2008/12/28/when-cache_classes-gets-you-down/)**
>
> As I’ve mentioned before, in Rails 2.2 if config.cache\_classes is true (which it is in production) then all of your models, controllers etc are …

Fred
