具有正确的Rails命名和约定的“PG :: UndefinedTable:ERROR:relation不存在”
我已经阅读了很多像这样的底池,但我见过的所有解决方案都在模型,命名和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 "id" validate :expense_date...
…
我有同样的问题,我发现在我的迁移中,我没有复数forms的表名:
例如:
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
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
。 它开始工作!!!!
我再次遇到了自己的错误!!,在执行rake test
,但是在这种情况下感谢cedricdlb我在这里找到了解决方案,非常简单:
rake db:test:prepare rake db:test:load