Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

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

...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...