使用不带rails的ActiveRecord连接到现有的Postgresql数据库



我没有使用rails,我正在尝试使用ActiveRecord来更容易地使用现有数据库。这只是一个脚本,所以我没有database.yml或任何其他文件。我已经使用设置了数据库连接

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

我对数据库不是很熟悉,所以我只想说明我对这个数据库的了解。有相当多的模式。我感兴趣的模式是具有表"host"的管理模式。我在脚本中创建了一个类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' # <-- ???
)

最新更新