12

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

...

Albert Català
  • 2,026
  • 2
  • 29
  • 35
  • Yes, I can see, but I don't understand what it means. What's wrong? – Albert Català Feb 05 '14 at 12:43
  • 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 05 '14 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 '14 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 '14 at 13:41

3 Answers3

8

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

bmalets
  • 3,207
  • 7
  • 35
  • 64
3

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
Community
  • 1
  • 1
Albert Català
  • 2,026
  • 2
  • 29
  • 35
0

I had the same issue, I realised that migrations should be sequential. For example, if you want likes to reference post, then you start by migrating post then likes. That was, you cant have issues. If you already have the migrations, do it manually by swapping the content of the files.

Kip
  • 11
  • 1