Order by a value of an arbitrary attribute in hstore

自古美人都是妖i 提交于 2020-01-04 04:47:17
问题 I have records like these: id, hstore_col 1, {a: 1, b: 2} 2, {c: 3, d: 4} 3, {e: 1, f: 5} How to order them by a maximum/minimum value inside hstore for any attribute? The result should be like this(order by lowest): id, hstore_col 1, {a: 1, b: 2} 3, {e: 1, f: 5} 2, {c: 3, d: 4} I know, I can only order them by specific attribute like this: my_table.hstore_fields -> 'a' , but it doesn't work for my issue. 回答1: Convert to an array using avals and cast the resulting array from text to ints.

Cast JSON to HSTORE in Postgres 9.3+?

本小妞迷上赌 提交于 2020-01-03 16:00:36
问题 I've read the docs and it appears that there's no discernible way to perform an ALTER TABLE ... ALTER COLUMN ... USING statement to directly convert a json type column to an hstore type. There's no function available (that I'm aware of) to perform the cast. The next best alternative I have is to create a new column of type hstore , copy my JSON data to that new column using some external tool, drop the old json column and rename the new hstore column to the old column's name. Is there a

unique index or constraint on hstore key

依然范特西╮ 提交于 2020-01-02 02:21:10
问题 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

No operator matches the given name and argument type(s)

╄→гoц情女王★ 提交于 2019-12-24 14:33:52
问题 I set up my Rails 3.2.x app to use PostgreSQL HStore but I'm getting an error. It looks like the hstore extension hasn't been picked up by the environment. I already rebooted my machine, checked database extensions, etc. When I try to execute: User.where("settings @> (:key => :value)", :key => "setting_x", :value => "test") I get an error: ( @> is not recognized, i.e., extension hstore is not installed?) HINT: No operator matches the given name and argument type(s). You might need to add

Race condition using Postgres hstore

我是研究僧i 提交于 2019-12-24 11:37:46
问题 I have a table which has amongst many other fields one hstore db/schema.rb create_table "requests", force: true do |t| t.hstore "parameters" end Some of the records have a field parameters["company_id"] but not all of them. What I need to do is to make sure that only one Request object is created with a given parameters["company_id"] . There might be multiple attempts trying to save a record at the same time - thus the race condition. I am looking for unique company_id values inside the

Can I use ActiveRecord relationships with fields from an Hstore?

对着背影说爱祢 提交于 2019-12-22 18:22:01
问题 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

simple_form and hstore basic functionality

两盒软妹~` 提交于 2019-12-22 18:15:33
问题 I can get hstore to work with simple_form but all but the most basic functionality (saving) just doesn't work. Validation messages don't show up on the individual fields... all hstore fields oddly show as required, even the values themselves don't populate correctly unless set manually. I have to do something like this: <%= f.simple_fields_for :phones do |phone| %> <%= phone.input :agent, :input_html => { :value => @artist.phones['agent'] } %> <% end %> I have to use simple_fields_for for the

Renaming hstore key in PostgreSQL 9.2

放肆的年华 提交于 2019-12-22 03:52:40
问题 I've been evaluating PostgreSQL's hstore functionality (9.2) and the only thing the fine manual isn't explicit about is how to rename keys. For instance, how could I rename the key c to ai_count ? "c"=>"3", "ai_voltage"=>"3", "ai_temperature"=>"28" I'm thinking there is no direct way to do this and that it involves duplicating the c key to a ai_count key, then dropping the c key. How can I do this, ideally as a one-liner which can be applied to multiple records? 回答1: I think you're right that

PG::UndefinedObject: ERROR: type “hstore” does not exist but it does

邮差的信 提交于 2019-12-19 19:59:26
问题 First of all, this may look like a duplicate of: postgres hstore exists and doesn't exist at same time but it is not. While I am getting the same error message in the circumstance. When checking to see if hstore is installed on the DB, we can see that it is: ./psql -d photographerio_development -c '\dx' List of installed extensions Name | Version | Schema | Description ---------+---------+------------+-------------------------------------------------- hstore | 1.2 | hstore | data type for

How to write into PostgreSQL hstore using Spark Dataset

折月煮酒 提交于 2019-12-19 08:31:31
问题 I'm trying to write a Spark Dataset into an existent postgresql table (can't change the table metadata like column types). One of the columns of this table is of type HStore and it's causing trouble. I see the following exception when I launch the write (here the original map is empty which when escaped gives an empty string): Caused by: java.sql.BatchUpdateException: Batch entry 0 INSERT INTO part_d3da09549b713bbdcd95eb6095f929c8 (.., "my_hstore_column", ..) VALUES (..,'',..) was aborted.