hstore

Can I use ActiveRecord relationships with fields from an Hstore?

醉酒当歌 提交于 2019-12-06 09:08:14
Can I tie a model to another through active record belongs_to using a field from an hstore hash? I'll elaborate: I have a User model that gets subclassed via STI on one of its fields to many different other User models based on permissions: class User < ActiveRecord::Base self.inheritance_column = :role #other definitions and validations end Here's one such submodel, the nightclub_boss model, meant for administrative users for my app: class NightclubBoss < User belongs_to :nightclub #the boss record has a nightclub_id has_one :rp_boss #rp_boss should have a nightclub_boss_id has_one :captain

Return records that don't have specific key - Rails4 HStore Postgresql query

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 05:20:39
I have a model Task t.string "name" t.hstore "actions" Example: #<Task id: 1, name: "first", actions: {"today"=>"9"}, #<Task id: 2, name: "second", actions: {"yesterday"=>"1"}, #<Task id: 3, name: "third", actions: nil, #<Task id: 4, name: "four", actions: {"today"=>"11"}, I need to find all records where actions: nil, :today<10 and key :today not present. It is 1,2,3 task. Task.where("actions -> 'today' < '10'") - it return only first task. Task.where("actions -> 'today' < '10' OR actions IS NULL") - it return 1 and 3 records. How i can find all records which do not have the key :today in

Create HSTORE with multiple schemas

霸气de小男生 提交于 2019-12-06 05:01:42
I have been trying to migrate my database to have HSTORE but the extension only works for public SCHEMA when I want to add an HSTORE column in other schemas it does not work def up # My hstore looks like this execute "CREATE EXTENSION hstore SCHEMA public" # I have also tried # execute "CREATE EXTENSION hstore" end but when I run my next migration it just doesn't work and if I go to psql console and alter tables I get this: set search_path to public; alter table accounts add column extras hstore; -- Works fine set search_path to schema2; alter table accounts add column extras hstore; -- Raises

Best PostgreSQL datatype for storing key-value maps?

送分小仙女□ 提交于 2019-12-06 03:17:25
问题 I'd like to store a simple map of key-value strings as a field in my PostgreSQL table. I intend to treat the map as a whole; i.e, always select the entire map, and never query by its keys nor values. I've read articles comparing between hstore , json and jsonb , but those didn't help me choose which data-type is most fitting for my requirements, which are: Only key-value, no need for nesting. Only strings, no other types nor null . Storage efficiency, given my intended use for the field. Fast

Convert a stringified array back to array

╄→尐↘猪︶ㄣ 提交于 2019-12-05 16:14:10
I use hstore with Postgres 9.2 and Rails 3.2 to store my object like this: class User user_hstore = {:user_id =>"123", :user_courses => [1,2,3]} end Now, when I retrieve user_courses, I get a string like this: '[1, 2, 3]' How do I convert this string to Rails array? Better yet, is there a way to store an array within a hstore object so that Rails will automatically retrieve it as array type? JSON.parse "[\"1018\", \"1037\", \"1045\", \"1042\"]" #=> ["1018", " 1037", " 1045", " 1042"] Mike Why not just use eval ? eval('[1, 2, 3]') #=> [1, 2, 3] Obviously, don't do this on arbitrary or user

unique index or constraint on hstore key

你离开我真会死。 提交于 2019-12-05 04:11:08
I would like to create a unique index or constraint on a specific keys in an hstore column if that key exists. I was hoping the answer would be found somewhere in this other question: Practical limitations of expression indexes in PostgreSQL But I tried every version of the syntax I could come up with and nothing would work. currently, my table is hstore_table the hstore field is hstore_value and they keys I would like to force to be unique are 'foo' and 'bar' when they exist. My version of PostgreSQL is 8.4.13 If I've understood what you're asking for correctly, you want a partial unique

Rails and Postgres Hstore: Can you add an index in a migration?

谁说我不能喝 提交于 2019-12-04 17:35:41
问题 I have a migration where I create a products table like so class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.string :name t.hstore :data t.timestamps end end end On the activerecord-postgres-hstore page they add an index to the table (in SQL) with CREATE INDEX products_gin_data ON products USING GIN(data); However that change is not tracked by migrations (I'm guessing because it's Postgres specific?), is there a way to create an index from within a

Select column name and value from table

拜拜、爱过 提交于 2019-12-04 16:21:36
If I have the following table in a PostgreSQL database: Col1 Col2 Col3 A a 1 B b 2 Is there a way to get the column name for each value without explicitly specifying the column names ? I.e. have a result set like: Col1 A Col1 B Col2 a Col2 b Col3 1 Col3 2 Erwin Brandstetter Of course, you could write a PL/pgSQL function and query the catalog table pg_attribute yourself. But it's so much easier with one of the following: JSON The function row_to_json() provides functionality that goes half the way. Introduced with Postgres 9.2 : SELECT row_to_json(t, TRUE) FROM tbl t; You don't have to mention

Dump and restore of PostgreSQL database with hstore comparison in view fails

穿精又带淫゛_ 提交于 2019-12-04 13:28:08
问题 I have a view which compares two hstore columns. When I dump and restore this database, the restore fails with the following error message: Importing /tmp/hstore_test_2014-05-12.backup... pg_restore: [archiver (db)] Error while PROCESSING TOC: pg_restore: [archiver (db)] Error from TOC entry 172; 1259 1358132 VIEW hstore_test_view xxxx pg_restore: [archiver (db)] could not execute query: ERROR: operator does not exist: public.hstore = public.hstore LINE 2: SELECT NULLIF(hstore_test_table

Can I store arrays in hstore with Rails

天涯浪子 提交于 2019-12-03 18:00:39
问题 I want to save data like this: User.create(name:"Guy", properties:{url:["url1","url2","url3"], street_address:"asdf"}) Can I do so in Rails 4? So far, I have tried migration: add_column :users, :properties, :hstore, array: true But when I save the array in hstore, it returns error: PG::InvalidTextRepresentation: ERROR: array value must start with "{" or dimension information 回答1: hstore is intended for simple key/value storage, where both the keys and values are simple unstructured strings.