我沒有使用rails,我試圖使用ActiveRecord使它更容易使用現有的數據庫。這只是一個腳本,所以我沒有database.yml或任何其他文件。我已經建立了我的數據庫連接使用用ActiveRecord連接到現有的Postgresql數據庫沒有rails
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'thehosthere',
database: 'management',
username: 'management_readonly',
password: '',
port: '5432'
)
我不是很熟悉數據庫,所以我只是說出我對這一個的瞭解。有相當多的模式。我感興趣的模式是具有表「主機」的管理模式。我創建了一個腳本類主持人是這樣的:
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,我知道我想要的表在管理中,那麼如何訪問它?
[使用與Rails的模型多個PostgreSQL模式]的可能重複(http://stackoverflow.com/questions/8806284/using-multiple-postgresql-schemas -with-rails-models) –
也許嘗試添加'schema_search_path:'management''給您的'establish_connection'調用? – maerics
嚴重的是,使用另一個ORM。任何其他ORM。 ActiveRecord是Rails中最糟糕的部分。作爲從數據庫方面接近它的人,它是......可怕的。 –