On Saturday, July 6, 2019 at 9:33:27 PM UTC+3, Jeremy Evans wrote:
On Saturday, July 6, 2019 at 5:01:31 AM UTC-7, Юрий Смирнов wrote:
I have a model with jsonb column and I use the pg_json extension. Here is 
the problem:

> model.update(x: { a: 1 })


> model.x
=> {:a=>1}

> model.previous_changes
=> {:x=>[{"a"=>1}, {:a=>1}]}

> model.reload.x
=> {"a"=>1}


So Sequel is converting the symbols into strings when saving data to jsonb 
column, but the model attribute value still contains symbols until I reload 
the model. This also makes previous_changes method to provide wrong results.
Maybe Sequel should update the attribute value after doing conversions?

You should never use symbols in JSON hashes, as explained in the pg_json 
documentation:

# Note that when accessing json hashes, you should always use strings for 
keys.
# Attempting to use other values (such as symbols) will not work correctly.

Thanks,
Jeremy

I understand, but the problem is not with accessing certain keys, but with 
tracking changes in my case. The previous_changes method returns the result 
that is not valid. I can also can have some symbols somewhere inside values 
and not the keys and still face the problem when after reloading I get the 
data that is different from what I get before that. 

I know I can workaround this by always calling as_json on stuff that I put 
into jsonb columns, but that's not very convenient and you can always 
forget doing that.

So maybe it would be better to implement the following behaviour? (Maybe as 
a plugin)

> model.update(x: { a: 1 })
> model.x # => { "a" => 1 } # autoconverted to "json representation"
> model.reload.x # => { "a" => 1 } # remains consistent

What do you think?

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/c2b80ac7-8713-44fc-9c49-fae9cd1cc779%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to