ruby - 使用不带导轨的 ActiveRecord 连接到现有的 Postgresql 数据库

标签 ruby database postgresql activerecord

我没有使用 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,我可以看到模式。这给了我一个数组 => ["db_stats", "management", "management_audit", "public"] 使用 pgadmin,我知道我想要的表在管理中,那么我如何访问那?

最佳答案

尝试在调用 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' # <-- ???
)

关于ruby - 使用不带导轨的 ActiveRecord 连接到现有的 Postgresql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18519366/

相关文章:

PostgreSQL 异常处理

postgresql - 为什么 PostgreSQL 使用逗号作为小数分隔符显示时间,即使 LC_NUMERIC 是 en_US.UTF-8

Ruby OpenSSL 非对称加密——使用两个 key 对

c# - 合并具有相同主键的行并计算新值的平均值

ruby - 显示 couchdb 文档的值

Java使用外键将数据插入数据库

database - 创建 Informix 函数时出错

PHP 和 PostgreSQL,在数据库中注册新用户的最佳实践?

ruby - 如何在许多匹配项中使用 ruby​​ gsub Regexp?

ruby-on-rails - 如何在 Ruby on Rails 中加入嵌套的 own_to