postgresql “PG::UndefinedTable:错误:关系不存在”具有正确的 Rails 命名和约定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21576686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:17:43  来源:igfitidea点击:

"PG::UndefinedTable: ERROR: relation does not exist" with a correct Rails naming and convention

ruby-on-railspostgresqlpostgresql-9.1

提问by Albert Català

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.

我读过很多这样的potsts,但我看到的所有解决方案都在模型的命名法、命名和Rails约定中。

Now I have this problem when I run for first time in production environment in PostgreSQL 9.1

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

    rake db:migrate RAILS_ENV=production

or

或者

    rake db:schema:load RAILS_ENV=production 

I could create database without problems: rake db:create RAILS_ENV=production=>OK

我可以毫无问题地创建数据库:rake db:create RAILS_ENV=production=>好的

The error is

错误是

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

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?

并且模型遵循所有 Rails 命名约定。所以,我不知道这个错误告诉我 什么?还有其他可能导致这个错误的东西吗?

The models:

型号:

class Category < ActiveRecord::Base
  has_many :subcategories
end

class Subcategory < ActiveRecord::Base
  belongs_to :category
end

shema.rb:

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

拐点.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'category', 'categories'
  inflect.irregular 'subcategory', 'subcategories'

  inflect.plural 'category', 'categories'
  inflect.plural 'subcategory', 'subcategories'
end

And remove the relationship of the models involved, like this:

并删除所涉及模型的关系,如下所示:

class ExpenseDetail < ActiveRecord::Base
  # belongs_to :expense
  # belongs_to :category
  # belongs_to :subcategory

  default_scope :order=>"id"

  validate :expense_date...

...

...

回答by bmalets

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

我有同样的问题,我发现在我的迁移中我没有复数形式的表名:

For example:

例如:



    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


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

我有create_table :post,但是当我将其更改为create_table :posts. 它开始工作!!!!

回答by Albert Català

I ran into my own error again !!, when executing rake test, but in this case thanks to cedricdlbI've found herethe solution, quite simply:

我再次遇到自己的错误!!,在执行时rake test,但在这种情况下,多亏了cedricdlb,我在这里找到了解决方案,很简单:

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