具有正确的Rails命名和约定的"PG :: UndefinedTable:ERROR:relation不存在"

Alb*_*alà 12 postgresql ruby-on-rails postgresql-9.1

我已经阅读了很多像这样的底池,但我看到的所有解决方案都在模型,命名和Rails惯例的命名中.

现在,当我在PostgreSQL 9.1中的生产环境中第一次运行时,我遇到了这个问题

    rake db:migrate RAILS_ENV=production
Run Code Online (Sandbox Code Playgroud)

要么

    rake db:schema:load RAILS_ENV=production 
Run Code Online (Sandbox Code Playgroud)

我可以毫无问题地创建数据库:rake db:create RAILS_ENV=production=>好的

错误是

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
Run Code Online (Sandbox Code Playgroud)

这些模型遵循所有Rails命名约定.所以,我不知道这个错误告诉我 什么?还有其他什么可以导致这个错误?

型号:

class Category < ActiveRecord::Base
  has_many :subcategories
end

class Subcategory < ActiveRecord::Base
  belongs_to :category
end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

最后,我尝试了这样的事情,没有成功

是inflections.rb

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

  inflect.plural 'category', 'categories'
  inflect.plural 'subcategory', 'subcategories'
end
Run Code Online (Sandbox Code Playgroud)

并删除所涉及的模型的关系,如下所示:

class ExpenseDetail < ActiveRecord::Base
  # belongs_to :expense
  # belongs_to :category
  # belongs_to :subcategory

  default_scope :order=>"id"

  validate :expense_date...
Run Code Online (Sandbox Code Playgroud)

...

bma*_*ets 7

我有同样的问题,我发现在我的迁移中,我没有复数形式的表名:

例如:



    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


我有create_table :post,但当我改变它create_table :posts.它开始工作!!!!