Tag: hstore

使用Postgres hstore的竞争条件

我有一个表,其中包括许多其他领域的一个hstore DB / schema.rb create_table “requests”, force: true do |t| t.hstore “parameters” end 一些记录具有字段parameters[“company_id”]但不是全部。 我需要做的是确保只使用给定parameters[“company_id”]创建一个Request对象。 可能有多次尝试同时保存记录 – 因此竞争条件。 我在整个表中的hstore寻找唯一的company_id值。 我发现我可以运行一个事务来锁定数据库并检查具有给定parameters[“company_id”]的请求是否存在,如果不创建它。 如果company_id是Request模型上的一个简单字段,我可以这样做: Request.transaction do if Request.find_by(company_id: *id* ) log_duplication_attempt_and_quit else create_new_record log_successful_creation end end 不幸的是它是hstore而我无法改变它。 使用hstore实现这一目标的最佳方法是什么? 我正在寻找快速的东西,因为表中有很多记录。 纯SQL查询没问题 – 遗憾的是我没有足够的SQL背景来弄明白我自己。 这可以为性能编制索引吗? 例: a = Request.new(parameters: {company_id: 567, name: “John”}) b = Request.new(parameters: {name: “Doesn’t have company_id […]

Postgres HStore错误 – 未知运算符

我的ruby代码: Portfolio.where(“data @> (:key => :value)”, :key => ‘CSJ’, :value => ‘0.1’) 生成以下SQL: “SELECT \”portfolios\”.* FROM \”portfolios\” WHERE (data @> (‘CSJ’ => ‘0.1’))” 想出这个错误: Error: PG::Error: ERROR: operator does not exist: unknown => unknown LINE 1: …olios”.* FROM “portfolios” WHERE (data @> (‘CSJ’ => ‘0.1’)) HINT: No operator matches the given name and argument type(s). […]

在Hash中保存错误排序后的activerecord-postgres-hstore

使用Hstore存储为哈希表,在保存后在哈希中输入错误 class Service 1, “zz” => 2, “cc” => 3, “d” => 4 } #=> { “aaa” => 1, “zz” => 2, “cc” => 3, “d” => 4 } service.save reload! service = Service.find(:id) service.properties #=> { “d” => “4”, “cc” => “3”, “zz” => 2, “aaa” => 1 } Bug::: wrong ordering after save […]

具有hstore属性的制造商

我正在尝试使用’制造’构建制造商,’2.8.1’具有hstore属性。 Fabricator(:inventory_item_change) do attribute_changes Hash.new(“num_units” => “to:50”) state “scheduled” change_code 1 execution_at Time.now.advance(days: 3) inventory_item end 这是我使用此制造商运行测试时收到的错误消息。 我已将问题隔离为hstore属性:属性更改。 Failure/Error: attr = Fabricate.attributes_for(:inventory_item_change) ArgumentError: bad value for range 任何人都可以协助识别正确的语法,或其他合适的解决方案来制作具有hstore属性的对象吗?

使用Rails 4(和Hstore)在PostgreSQL中存储嵌套的哈希

我有一个Rails应用程序,它汇集了来自Google API的大量数据。 我目前在MongoDB中存储JSON响应(因此我的Rails应用程序同时包含pg和mongo)。 但是,今天,我遇到了PostgreSQL Hstore扩展,我决定尝试一下。 不幸的是,我遇到了一个问题。 API提供的JSON是多层深度的,因此JSON.parse之后的Ruby Hash包含哈希,其中包含新的哈希值。 但是, Hstore是字符串键/值存储,它只有1级深度。 所以第一个哈希中的哈希就变成了字符串。 我发现真正讨厌的黑客是eval变成字符串的哈希: eval(“{ “foo” => “bar” }”) 我不喜欢这个。 关于该怎么做的任何提示? 我应该继续使用MongoDB还是有更好的方法在PG中存储多级深度哈希值?

返回没有特定键的记录 – Rails4 HStore Postgresql查询

我有一个模型任务 t.string “name” t.hstore “actions” 例: #”9″}, #”1″}, #<Task id: 3, name: "third", actions: nil, #”11″}, 我需要找到行动的所有记录:nil,:今天<10和关键:今天不存在。 这是1,2,3任务。 Task.where(“actions -> ‘today’ ‘today’ < '10' OR actions IS NULL") – it return 1 and 3 records. 我怎么能找到所有没有钥匙的记录:今天在行动?

将字符串化数组转换回数组

我使用hstore与Postgres 9.2和Rails 3.2来存储我的对象,如下所示: class User user_hstore = {:user_id =>”123″, :user_courses => [1,2,3]} end 现在,当我检索user_courses时,我得到一个这样的字符串: ‘[1, 2, 3]’ 如何将此字符串转换为Rails数组? 更好的是,有没有办法在hstore对象中存储数组,以便Rails自动将其作为数组类型检索?

Rails和Postgres Hstore:你能在迁移中添加索引吗?

我有一个迁移,我创建了一个像这样的产品表 class CreateProducts < ActiveRecord::Migration def change create_table :products do |t| t.string :name t.hstore :data t.timestamps end end end 在activerecord-postgres-hstore页面上,他们为表添加一个索引(在SQL中) CREATE INDEX products_gin_data ON products USING GIN(data); 然而,迁移不会跟踪这种变化(我猜是因为它是Postgres特定的?),有没有办法从迁移中创建索引? 谢谢!

PG :: UndefinedObject:错误:类型“hstore”不存在但它确实存在

首先,这可能看起来像是重复: postgres hstore存在且不存在于同一时间 但事实并非如此。 虽然我在这种情况下收到相同的错误消息。 在检查数据库中是否安装了hstore时,我们可以看到它是: ./psql -d photographerio_development -c ‘\dx’ List of installed extensions Name | Version | Schema | Description ———+———+————+————————————————– hstore | 1.2 | hstore | data type for storing sets of (key, value) pairs plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language 它也在template_1 DB上。 所以,当我尝试运行迁移来添加hstore时,我得到PG::Error: ERROR: extension “hstore” already exists […]

在rails4中设置hstore,动态键/值

我在rails4应用程序中第一次使用Hstore,我在表单中使用javascript来构建hstore列的动态表单字段(:schema) 在rails 4中,我不需要在我的模型中添加任何setter / getter方法,对吗? 在我的表单中,我正在构建动态输入字段,并允许用户设置键/值对。 很像Hstore Heroku演示应用程序 所以基本上我的表格会有输入 input name=”app[schema][dynamic_key1]” value=”whatever value” input name=”app[schema][dynamic_key2]” value=”whatever value2″ 在我的App Controller中: def app_params params.require(:app).permit(:name, :title, :schema ) end 但是,当我创建新的App记录时,我的架构hstore值不会保存。 我看到了一些关于制作强大的参数的东西:schema => []但是仍然无效。 由于我不知道这些值是什么,我无法为这些设置store_accessors,就像我在很多例子中看到的那样。