“ PG :: UndefinedTable:错误:关系不存在”正确的Rails命名和约定 [英] "PG::UndefinedTable: ERROR: relation does not exist" with a correct Rails naming and convention

查看:90
本文介绍了“ PG :: UndefinedTable:错误:关系不存在”正确的Rails命名和约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过很多类似的文章,但是我看到的所有解决方案都在模型的命名,命名和Rails约定中。

I've read a lot of potsts like this, but all the solutions I've seen are in the nomenclature of the models, naming and Rails convention.

现在,当我我在PostgreSQL 9.1的生产环境中首次运行

    rake db:migrate RAILS_ENV=production

    rake db:schema:load RAILS_ENV=production 

我可以创建数据库没有问题: rake db:create RAILS_ENV = production => OK

I could create database without problems: 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命名约定。因此,我不知道此错误告诉我
?还有其他原因可以导致此错误吗?

And the models follows all Rails naming convention. So that, I don't know what this error is telling me ¿There is something else that can cause this error?

模型:

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

最后,我尝试了类似的方法,但没有成功

And finally, I tried something like this, without success

inflections.rb

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...

...

推荐答案

我遇到了同样的问题,并且发现在迁移过程中,我没有复数形式的表名:

I have the same problem and I found that in my migrations I don't have table names in plural form:

例如:




    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 时。
它开始工作!!!!!

I have create_table :post, but when I change it to create_table :posts. It start working!!!!

这篇关于“ PG :: UndefinedTable:错误:关系不存在”正确的Rails命名和约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆