Подтвердить что ты не робот

"PG:: UndefinedTable: ERROR: отношения не существует" с правильным названием и условным названием Rails

Я читал много таких горшков, но все решения, которые я видел, представлены в номенклатуре моделей, имен и соглашений 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...

...

4b9b3361

Ответ 1

У меня такая же проблема, и я обнаружил, что в моих миграциях у меня нет имен таблиц во множественном числе:

Например:



    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. Он начинает работать!!!!

Ответ 2

Я снова столкнулся с моей собственной ошибкой!!, при выполнении rake test, но в этом случае благодаря cedricdlb я нашел здесь решение, довольно просто

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