我看过很多类似的帖子,但是我见到的所有解决方案都涉及到模型的命名和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...
...