Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG:Error relation does not exist

I am using Rails 3.2.x and also using RailsAdmin.

I have run many migrations and changed my DB structure many times. As a result of that, I am getting a PG:Error saying that a relation doesn't exist for a table that really doesn't exist.

This is the error:

ActiveRecord::StatementInvalid at /
PG::Error: ERROR:  relation "addresses" does not exist
LINE 5:              WHERE a.attrelid = '"addresses"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod

So in short, when I go to /admin the error shown in my development.log is this:

Started GET "/admin" for 127.0.0.1 at 2013-07-04 02:24:07 -0500
Processing by RailsAdmin::MainController#dashboard as HTML
  Cart Load (0.5ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT 1  [["id", 5]]
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
   (0.6ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
   (0.6ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 1 AND (((roles.name = 'seller') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
PG::Error: ERROR:  relation "addresses" does not exist
LINE 5:              WHERE a.attrelid = '"addresses"'::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 = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Completed 500 Internal Server Error in 1269ms

ActiveRecord::StatementInvalid - PG::Error: ERROR:  relation "addresses" does not exist
LINE 5:              WHERE a.attrelid = '"addresses"'::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 = '"addresses"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

How do I fix this?

like image 228
marcamillion Avatar asked Jul 04 '13 07:07

marcamillion


People also ask

How do you fix relation does not exist in PostgreSQL?

The name of table my_database referencing a column in my_schema does not exist because you need to add CREATE TABLE statement to create a table. Now by adding the statement below, we can make sure that we don't get this comment relation does not exist.

What is a Postgres relation?

PostgreSQL is a relational database management system ( RDBMS ). That means it is a system for managing data stored in relations. Relation is essentially a mathematical term for table.

Does not exist Sequelize?

In PostgreSQL, a relation does not exist error happens when you reference a table name that can't be found in the database you currently connect to. In the case above, the error happens because Sequelize is trying to find Users table with an s , while the existing table is named User without an s .


1 Answers

What I would suggest is to drop and recrete the whole db. You probably will have some problems in migrations while doing this - fix them all so a clean install of an app is possible.

like image 51
Mike Szyndel Avatar answered Oct 07 '22 14:10

Mike Szyndel