-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update single attribute in hash column #45
Comments
@NomNomCameron this is interesting, and certainly, if possible, it should be done. The question is where. First would be to find out if Postgres allows this. If it does, we should check if |
As of this article (last updated: 2018-02-27) this should be possible:
having
I quickly tested this (rails 5.2, ruby 2.5.1, storext 2.2.2) and it works as described in this article. So one can update a single value within the jsonb without destroying any other fields. However something like If this answers the questions, we can close this issue? :) |
Yes, thanks. I haven't had time to take a look at it yet, but I will in a
few weeks!
…On Thu, Sep 27, 2018 at 7:50 AM Lud ***@***.***> wrote:
as of this article
<https://blog.codeship.com/unleash-the-power-of-storing-json-in-postgres/>
this should be possible:
irb(main):004:0> card.data["finished"] = true
=> true
irb(main):005:0> card.save
(0.2ms) BEGIN
SQL (0.9ms) UPDATE "cards" SET "data" = $1 WHERE "cards"."id" = $2 [["data", "{\"name\":\"Organize Kitchen\",\"tags\":[\"Fast\",\"Organize\",\"Carpet\",\"Cook\",\"Kitchen\"],\"finished\":true}"], ["id", 1]]
(6.6ms) COMMIT
=> true
having
create_table :cards do |t|
t.integer :board_id, null: false
t.jsonb :data
end
I quickly tested this (rails 5.2, ruby 2.5.1, storext 2.2.2) and it works
as described in this article. However something like card.update!("data->>'foo'":
'bar') raises UnknownAttributeError.
If this answers the questions, we can close this issue? :)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#45 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AABi9NuawrGI26wgh5lS_Ls06pLnpODUks5ufBLSgaJpZM4QqqlE>
.
|
Obviously I never took a look, or I forgot that I did. If you've worked on it I'd love to accept a PR |
Currently use storext on jsonb columns on my models and just realized that when updating the hash column with certain values for keys I want to update, it destroys the rest of the hash (because you're essentially setting that column to equal the new hash you're passing in)
If it would be useful to the rest of the community here I'd be willing to open a pull request that enables the ability to update specific values for some keys while preserving the values of keys not passed in when updating the column storext is on.
I can also send an example if what I'm trying to achieve isn't clear.
The text was updated successfully, but these errors were encountered: