Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

"PG::UndefinedTable: ERROR: relation does not exist" with a correct Rails naming and convention

Ask Question

I've read a lot of potsts like this, but all the solutions I've seen are in the nomenclature of the models, naming and Rails convention.

Now I have this problem when I run for first time in production environment in PostgreSQL 9.1

    rake db:migrate RAILS_ENV=production
    rake db:schema:load RAILS_ENV=production 

I could create database without problems: rake db:create RAILS_ENV=production =>OK

The error is

rake aborted!
PG::UndefinedTable: ERROR:  relation "categories" does not exist
LINE 5:                WHERE a.attrelid = '"categories"'::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 = '"categories"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

And the models follows all Rails naming convention. So that, I don't know what this error is telling me ¿There is something else that can cause this error?

The models:

class Category < ActiveRecord::Base
  has_many :subcategories
class Subcategory < ActiveRecord::Base
  belongs_to :category

shema.rb:

ActiveRecord::Schema.define(version: nnnn) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
  create_table "categories", force: true do |t|
    t.string   "name"
    t.text     "comments"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.decimal  "amount_need_comments",           precision: 6, scale: 2
    t.decimal  "amount",                         precision: 6, scale: 2
    t.decimal  "amount_per_unit",                precision: 6, scale: 2
    t.integer  "teletrabajo",          limit: 2,                         default: 0, null: false
    t.decimal  "amount_need_city",               precision: 6, scale: 2
  create_table "subcategories", force: true do |t|
    t.string   "name"
    t.text     "comments"
    t.integer  "category_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.decimal  "amount_need_comments", precision: 6, scale: 2
    t.decimal  "amount",               precision: 6, scale: 2
    t.decimal  "amount_per_unit",      precision: 6, scale: 2
    t.decimal  "amount_need_city",     precision: 6, scale: 2

And finally, I tried something like this, without success

inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'category', 'categories'
  inflect.irregular 'subcategory', 'subcategories'
  inflect.plural 'category', 'categories'
  inflect.plural 'subcategory', 'subcategories'

And remove the relationship of the models involved, like this:

class ExpenseDetail < ActiveRecord::Base
  # belongs_to :expense
  # belongs_to :category
  # belongs_to :subcategory
  default_scope :order=>"id"
  validate :expense_date...
                Sorry.. but that double quoted is in the error, but not in my code.  I need to find in my code what is wrong
– Albert Català
                Feb 5, 2014 at 13:04
                Hi Albert, did you ever find a solution to this?  I have a very similar problem and after 3 days, I still can't figure it out!  What did you do? Thanks
– jfdimark
                Mar 13, 2014 at 11:27
                I couldn't solve for the ´rake db:migration´ or ´rake db:schema:load´. What I did, is make a backup of the development database and restore it to the production database. But in both case my PGSql database (develpment and production) are for development, si I did not run any risk.
– Albert Català
                Mar 13, 2014 at 13:41

I have the same problem and I found that in my migrations I don't have table names in plural form:

For example:

Is not my case, I have in plural form: create_table :subcategories do |t| maybe the problem was in t.references :category – Albert Català Jun 20, 2014 at 7:53

I ran into my own error again !!, when executing rake test, but in this case thanks to cedricdlb I've found here the solution, quite simply:

rake db:test:prepare
rake db:test:load
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.