Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

or

    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
end

class Subcategory < ActiveRecord::Base
  belongs_to :category
end

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
  end

  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
  end

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'
end

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...

...

like image 909
Albert Català Avatar asked Feb 05 '14 12:02

Albert Català


2 Answers

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

For example:



    class CreatePosts  ActiveRecord::Migration
      def change
        create_table :posts do |t|
          t.string :source
          t.string :destination
          t.datetime :time
          t.timestamps
        end
      end
    end


I have create_table :post, but when I change it to create_table :posts. It start working!!!!

like image 161
bmalets Avatar answered Oct 17 '22 06:10

bmalets


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
like image 3
Albert Català Avatar answered Oct 17 '22 07:10

Albert Català