Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails rake db:schema:load fails with new database

I ran rake db:drop (successfully) and ran rake db:create (successfully) but rake db:schema:load is throwing a strange error that I can't figure out.

** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted! PG::UndefinedTable: ERROR:  relation "admins" does not exist
LINE 5: WHERE a.attrelid = '"admins"'::regclass
                                      ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                    pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
               FROM pg_attribute a LEFT JOIN pg_attrdef d
                 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
           WHERE a.attrelid = '"admins"'::regclass
                AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Any suggestions?

like image 249
kirley Avatar asked Nov 09 '13 01:11

kirley


3 Answers

If you are using FactoryBot (FactoryGirl) and your factory contains any model type references, make sure they are enclosed in curly braces. That prevents db:schema:load from blowing up and you don't have to comment out your factory.

For example, let's say you have a Car model with wheels and an engine, and you have 2 types of engines in your seed data and are using traits to specify the engine type.

This code will blow up db:schema:load:

FactoryBot.define do
  factory :car do
    wheels

    trait(:v8) {
      engine Engine.find_by_type('V8') 
    }

    trait(:i4) {
      engine Engine.find_by_type('I4') 
    }
  end
end

This will prevent db:schema:load from blowing up:

FactoryBot.define do
  factory :car do
    wheels

    trait(:v8) {
      engine { Engine.find_by_type('V8') }
    }

    trait(:i4) {
      engine { Engine.find_by_type('I4') }
    }
  end
end

Here is a discussion about it: https://github.com/thoughtbot/factory_bot_rails/issues/134

UPDATE: This can happen when specifying the class for the factory as well:

This code will blow up db:schema:load:

FactoryBot.define do
  factory :hot_rod, class: Car do

This will prevent db:schema:load from blowing up (uses symbol instead of model type):

FactoryBot.define do
  factory :hot_rod, class: :car do
like image 66
ryan0 Avatar answered Nov 02 '22 19:11

ryan0


I don't totally understand why, but rake db:schema:load was having problems with one of my FactoryGirl files. I temporarily deleted file and rake db:schema:load worked fine.

The problem was caused by an incorrectly defined FactoryGirl association. Still not sure why factory girl initialized during rake db:schema:load.

like image 2
kirley Avatar answered Nov 02 '22 19:11

kirley


I had an issue with this on a project I inherited. It was to do with routing constraints that were loading models from the database e.g.

constraints(foobar: /#{FooBar.pluck(:name).join('|')}|other/) do

To get started quickly I just commented the block, ran the rake task then uncommented the block and everything was golden. Obviously not the issue of the original question, but a fix for the same error with slightly different problem due to loading problems.

like image 2
dft Avatar answered Nov 02 '22 18:11

dft