Carlo Teran
Carlo Teran

Reputation: 73

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "users" does not exist

I am trying to run an existing Rails application (cloned from Github), but when I try to do db:migrate, I get the following error. I get the same error when I run db:purge, db:schema:load, etc...

This is the error:

rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation 
"users" does not exist
LINE 8:                WHERE a.attrelid = '"users"'::regclass
                                      ^
:               SELECT a.attname, format_type(a.atttypid, 
a.atttypmod),
                 pg_get_expr(d.adbin, d.adrelid), a.attnotnull, 
a.atttypid, a.atttypmod,
                 c.collname, col_description(a.attrelid, a.attnum) AS 
comment
            FROM pg_attribute a
            LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND 
a.attnum = d.adnum
            LEFT JOIN pg_type t ON a.atttypid = t.oid
            LEFT JOIN pg_collation c ON a.attcollation = c.oid AND 
a.attcollation <> t.typcollation
           WHERE a.attrelid = '"users"'::regclass
             AND a.attnum > 0 AND NOT a.attisdropped
           ORDER BY a.attnum

Upvotes: 2

Views: 2539

Answers (1)

brainbag
brainbag

Reputation: 1057

When cloning a Rails project and setting up the database, you don't want to run the migrations. Migrations are intended for incremental database updates, and it's quite likely that they get old and don't run anymore. You want to run rake db:schema:load, which directly loads the db/schema.rb file into the database.

If you want a shortcut to running the seeds as well, you can use rake db:setup which creates the database (db:create), loads the schema (db:schema:load), then runs the seeds (db:seed). You can also do rake db:reset which does the same thing but drops the database first.

For more info see the Rails Guides on this topic: http://guides.rubyonrails.org/active_record_migrations.html#what-are-schema-files-for-questionmark

There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.

Upvotes: 2

Related Questions