jackc / surus

PostgreSQL extensions for ActiveRecord

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

undefined method 'hstore'

marcgreenstock opened this issue · comments

Hi Jack,

I'm getting an undefined method 'hstore' when trying to migrate my database.

The execute("CREATE EXTENSION hstore") is migrating first.

Here is my migration:
create_table :searches do |t|
t.hstore :properties
t.timestamps
end

Any help would be appreciated.

Postgres version is 90103

At this point there is only a migration generator to install the hstore extension. So you would need to drop down to SQL in your migration to create a table. I usually write my migrations directly in SQL as I target PostgreSQL exclusively for most of my projects. The Rails migration DSL is nice when targeting multiple DBMSs, but I don't find it useful when I am trying to target functionality specific to one. I believe the following code is equivalent to what you were trying to do.

execute <<-END_SQL
CREATE TABLE searches(
  id serial PRIMARY KEY,
  properties hstore,
  created_at timestamptz,
  updated_at timestamptz
);
END_SQL

You will also want to adjust the Rails config to use the SQL schema dumper. (It should generate structure.sql instead of schema.rb) Otherwise it will not properly clone your development structure into the test database.

I'm not opposed to adding the column migration helper if someone wants to contribute it, but seeing as Rails 4 will include hstore support out of the box, I think raw SQL is good enough for now.

Thanks! I moved away from using hstore for now. Since the project doesn't actually need the hstore functionality. I just thought it would be a nice-to-have.

I took a look at activerecord-postgres-hstore which does provide the helpers but doesn't serialize nested objects. Lets hope rails 4 has the best of both worlds.