使用正确的Rails命名和规范,出现"PG::UndefinedTable: ERROR: relation does not exist"错误。

12

我看过很多类似的帖子,但是我见到的所有解决方案都涉及到模型的命名和Rails惯例。

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

    rake db:migrate RAILS_ENV=production

或者

    rake db:schema:load RAILS_ENV=production 

我能够成功创建数据库:rake db:create RAILS_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

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

最后,我尝试了以下类似的方法,但没有成功。
inflections.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...

...


是的,我看得到,但我不明白它的意思。怎么了? - Albert Català
抱歉,但是这个双引号在错误信息中出现了,而不是我的代码里。我需要在我的代码中找出问题所在。 - Albert Català
嗨,阿尔伯特,你解决这个问题了吗?我有一个非常类似的问题,三天过去了,我还是无法解决它!你做了什么?谢谢。 - jfdimark
我无法解决´rake db:migration´或´rake db:schema:load´的问题。我的做法是备份开发数据库并将其恢复到生产数据库中。但在这两种情况下,我的PGSql数据库(开发和生产)都是用于开发,因此我没有冒任何风险。 - Albert Català
3个回答

8
我遇到了同样的问题,后来发现在我的迁移文件中,表名没有采用复数形式:
例如:


    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 时,它开始正常工作!!!

不是我的问题,我使用复数形式:create_table :subcategories do |t|,也许问题出在 t.references :category 上。 - Albert Català

3

我又遇到了自己的错误,当执行rake test时,但这一次多亏了cedricdlb,我在这里找到了解决方案,非常简单:

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

0

我曾经遇到过同样的问题,后来发现迁移应该是顺序进行的。例如,如果您想让“赞”引用“帖子”,那么您应该首先迁移“帖子”,然后再迁移“赞”。这样就不会出现问题了。如果您已经有了迁移文件,可以通过手动交换文件内容来解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接