If somebody could help me with Ecto/SQL I would really really appreciate it!
I have an embeds_many
schema and in the DB it’s a jsonb or array of json objects.
I need to update all objects in jsonb with a certain criteria, like:
if obj.id == 1 or or obj.name == "Old Name", update field "name:" to a "New Name"
So I am able to GET all needed objects by:
from(p in Post,
inner_lateral_join:
item in fragment(
"SELECT * FROM jsonb_to_recordset(to_jsonb(?)) as item(id integer, name text)",
c.comments
),
select: %Comment{
id: item.id,
name: item.name
},
where: item.id == 1 or item.name == "Old Name"
)
|> Repo.all()
So I got them:
[
%Comment{
id: 1,
name: "Old Name"
},
%Comment{
id: 2,
name: "Old Name"
}
]
But how can do the actual UPDATE? I guess raw SQL should be involved here somehow, but I wish I would know…
Thank you all in advance!
P.S. I asked the same question in Elixir Slack. Sorry if you read it again here!