Ruby on rails &引用;PG::UndefinedTable:错误:关系不存在“;使用正确的Rails命名和约定

Ruby on rails &引用;PG::UndefinedTable:错误:关系不存在“;使用正确的Rails命名和约定,ruby-on-rails,postgresql,postgresql-9.1,Ruby On Rails,Postgresql,Postgresql 9.1,我读过很多类似的POTST,但我看到的所有解决方案都是模型命名、命名和Rails约定 现在,当我第一次在PostgreSQL 9.1的生产环境中运行时,我遇到了这个问题 rake db:migrate RAILS_ENV=production 或 我可以毫无问题地创建数据库:rakedb:createrails\u ENV=production=>OK 错误是 rake aborted! PG::UndefinedTable: ERROR: relation "categories"

我读过很多类似的POTST,但我看到的所有解决方案都是模型命名、命名和Rails约定

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

    rake db:migrate RAILS_ENV=production

我可以毫无问题地创建数据库:
rakedb:createrails\u ENV=production
=>OK

错误是

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
模型遵循所有Rails命名约定。所以,我不知道这个错误告诉了我什么 还有其他原因会导致此错误吗?

模型:

class Category < ActiveRecord::Base
  has_many :subcategories
end

class Subcategory < ActiveRecord::Base
  belongs_to :category
end
最后,我尝试了类似的方法,但没有成功

屈折变化.rb

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

  inflect.plural 'category', 'categories'
  inflect.plural 'subcategory', 'subcategories'
end
并删除所涉及模型之间的关系,如下所示:

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

  default_scope :order=>"id"

  validate :expense_date...
class ExpenseDetail“id”
验证:费用\u日期。。。

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

例如:



    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\u table:post
,但当我将其更改为
create\u table:posts
时。
它开始工作了

我又犯了自己的错误!!,执行
rake测试时
,但在这种情况下,由于cedricdlb,我找到了解决方案,非常简单:

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

是的,我看得出来,但我不明白这是什么意思。怎么了?对不起。。但双引号出现在错误中,但不在我的代码中。我需要在我的代码中找出什么是错误的嗨,阿尔伯特,你找到解决这个问题的方法了吗?我有一个非常类似的问题,3天后,我仍然无法解决它!你做了什么?ThanksI无法解决“rake db:migration”或“rake db:schema:load”问题。我所做的是备份开发数据库并将其恢复到生产数据库。但在这两种情况下,我的PGSql数据库(开发和生产)都是为了开发,我没有任何风险。不是我的情况,我有复数形式:create_table:subcategories do | t |可能问题出在t.references:category


    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

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