Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Update always sets hstore column #102

Open
izoomi opened this issue May 3, 2013 · 5 comments
Open

Update always sets hstore column #102

izoomi opened this issue May 3, 2013 · 5 comments
Assignees

Comments

@izoomi
Copy link

izoomi commented May 3, 2013

When I update other fields, the hstore column, is always set as well.

post = Post.create(title: 'Title')
post.update_attribute(:title, 'Title2')
# (0.2ms)  BEGIN
# (0.4ms)  UPDATE "posts" SET "title" = 'Title2', "updated_at" = '2013-05-03 16:01:43.841376', 
# "options" = '' WHERE "posts"."id" = 6100
# (1.6ms)  COMMIT
# => true

The options column gets updated as well.

@ghost ghost assigned diogob May 6, 2013
@diogob
Copy link
Owner

diogob commented May 6, 2013

I'll take a look at this during this week

@sjmadsen
Copy link

sjmadsen commented May 7, 2013

Rails core is dealing with the flip side of this issue: rails/rails#6127

The problem is that if options already has some stuff in it, and you update a single key within it, you want post.save to update the options column, too.

Rails's change tracking doesn't work at the granularity of keys within a hash, so it's either:

  1. hstore columns are always updated or
  2. only when the entire attribute is assigned

In the latter case, a partial update won't be saved unless the developer is careful (actually reassigning the attribute or calling #options_will_change! everywhere a partial update occurs).

@edbond
Copy link

edbond commented Apr 14, 2014

Even if you don't change anything in hstore column, #save issue UPDATE .....
How to fix the bug?

@staszek-arsdata
Copy link

I've ended up with moving hstore column to separate relation. So now if need to access hstore attributes, will call them directly, by product.attrs.data instead of product.attrs.

Before:

class Product
  serialize :attrs, ActiveRecord::Coders::Hstore
end

After:

class Product
  has_one :attrs, class_name: 'ProductAttrs'
end
class ProductAttrs
  belongs_to :product
  serialize :data, ActiveRecord::Coders::Hstore
end

It's not so efficient on selects, but I do many more updates, than selects, so it works for me.

@OctoberComstock
Copy link

Has there been any update on this? I've run into an issue where hstore is removing columns in my shared pg database when a different api does an update request. The columns aren't defined in one api but are defined in the other, shouldn't those columns remain concrete and not be removed from the table?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants