【问题标题】:Connect to existing Postgresql database with ActiveRecord without rails使用不带导轨的 ActiveRecord 连接到现有的 Postgresql 数据库
【发布时间】:2013-09-02 09:12:54
【问题描述】:

我没有使用 Rails,而是尝试使用 ActiveRecord 来更轻松地使用现有数据库。这只是一个脚本,所以我没有 database.yml 或任何其他文件。我已经使用

设置了我的数据库连接
ActiveRecord::Base.establish_connection(
  adapter:    'postgresql',
  host:       'thehosthere',
  database:   'management',
  username:   'management_readonly',
  password:   '',
  port:       '5432'
)

我对数据库不是很熟悉,所以我只说一下我对这个数据库的了解。有很多模式。我感兴趣的架构是具有“主机”表的管理架构。我在脚本中创建了一个 Host 类,如下所示:

class Host < ActiveRecord::Base
    self.table_name = "host"
end

然后,我尝试使用此查询提取与条件匹配的所有行并将其存储到数组中。

servers = Host.where(:realm => 'stage', :status => 'UP').pluck(:hostname)

但我每次都收到此错误。

 ruby environments_are_okDev.rb
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec': PG::UndefinedTable: ERROR:  relation "host" does not exist (ActiveRecord::StatementInvalid)
LINE 5:                WHERE a.attrelid = '"host"'::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 = '"host"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_no_cache'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:138:in `block in exec_query'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:915:in `column_definitions'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:174:in `columns'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `default'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:208:in `columns'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:247:in `column_names'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `block in method_missing'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in `scoping'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `method_missing'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:152:in `block in pluck'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `map!'
        from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `pluck'
        from environments_are_okDev.rb:51:in `run'
        from environments_are_okDev.rb:97:in `<main>'

我真的不明白为什么会发生这种情况,所以我尝试使用此命令查看数据库的外观 ActiveRecord::Base.connection.tables 但我得到的只是一个空数组。我不确定会发生什么。我显然做错了什么,我只是不知道是什么。请记住,这一切都在一个文件脚本中。

编辑:

所以如果我使用ActiveRecord::Base.connection.schema_names,我可以看到模式。这给了我一个数组=&gt; ["db_stats", "management", "management_audit", "public"] 使用pgadmin,我知道我想要的表在管理中,那么我该如何访问它?

【问题讨论】:

  • 也许可以尝试将schema_search_path: 'management' 添加到您对establish_connection 的呼叫中?
  • 说真的,使用另一个 ORM。任何其他 ORM。 ActiveRecord 是 Rails 中最糟糕的部分。作为一个从数据库方面接近它的人,它......很可怕。

标签: ruby database postgresql activerecord


【解决方案1】:

尝试在拨打establish_connection时添加schema_search_path

ActiveRecord::Base.establish_connection(
  adapter:    'postgresql',
  host:       'thehosthere',
  database:   'management',
  username:   'management_readonly',
  password:   '',
  port:       5432,
  schema_search_path: 'management' # <-- ???
)

【讨论】:

  • 在您发布此内容之前,我已经知道我可以使用 ActiveRecord::Base.connection.schema_search_path,但我没有意识到我可以将它放在建立连接方法中。谢谢!
  • @maerics:我也有同样的问题,我试过你的解决方案,但它对我不起作用,你能不能给我发任何其他的替代方案
  • @anusha 您可以创建一个 before_filter 方法并将更改 schema_search_path 的逻辑放在那里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多