Stefan Fountain
Stefan Fountain

Reputation: 361

After upgrading Postgres and Rails I cannot get my migrations back on track

I had a working setup using Rails 3.1 and Postgres 9.0 and just upgraded Rails to 3.2.1 and Postgres 9.1.2.

Upgrading postgres meant migrating the data as the 2 versions datastores are incompatible. I didn't care much to migrate the data in my local db's as they are all used for development purposes anyway. So I'm trying to recreate my databases using Rails migrations (or w/ schema). However no matter what I try I cannot get the migrations to run.

Using either

rake db:migrate

or

rake db:reset

Running this on with my Postgres 9.1.2 setup gives me:

spif: > rake db:migrate
rake aborted!
PG::Error: ERROR:  relation "users" does not exist
LINE 4:              WHERE a.attrelid = '"users"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
               FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"users"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

At first I though it might be that the pg gem doesn't support Rails 3.2.1 yet so tried with sqlite3 as well but that gives me the same users not found:

spif: > rake db:reset
rake aborted!
Could not find table 'users'

So it looks like Rails expects a users table somewhere.

  1. I have no clue how to find out what code is actually calling this. When I run the migration commands with --trace it show's that is ActiveRecord internals doing it. Can this be a bug in ActiveRecord 3.2.1 (how would I go about finding that out?).

  2. I can connect to the pg db's fine using psql and my user setup in config/database.yml. Does rails expect some postgres tables that it can query about users?

  3. I have completely removed my db/migrate directory and the db/schema.rb in order to make sure there is not some strange internal dependency there but also to no avail.

I'm sure I'm missing some simple concept - I haven't often tried to reset my db from scratch so I hope there is a simple explanation.

Thanks, help very much appreciated.

Upvotes: 1

Views: 1064

Answers (2)

tbem
tbem

Reputation: 605

DO you use any model load in initializers? I had the same problem and i was loading the model in an initializer, so the error comes, because the table doesn't exist before doing the migration and i was trying to load something that ever doesn't exist!

Upvotes: 0

Stefan Fountain
Stefan Fountain

Reputation: 361

I solved this by manually creating the users table and each other table I had in this project. Then ran the migrations again and everything is setup fine. There were only a few tables so it wasn't too cumbersome but not the "fix" I was trying to find.

Looks like 'mu is too short' was right that this is related to ActiveAdmin and the way it loads itself when loading the environment during any db operation (or rails server/console too for that matter).

Thanks for the pointers mu.

Upvotes: 1

Related Questions