Equivalent of Update on Model on non primary key #1423
Unanswered
shalabgoel
asked this question in
Q&A
Replies: 1 comment
-
You would likely have to craft this query yourself: If you only need to update and don't necessarily care about the returned objects: err := models.Users(
qm.Where("uuid = ?", pt.UUID), qm.Where("c_id = ?", pt.CID),
).UpdateAll(ctx, exec, models.M{"days": pt.Days}) If you want to return the values: q := models.Users(
qm.Where("uuid = ?", pt.UUID), qm.Where("c_id = ?", pt.CID),
qm.Returning(...),
)
queries.SetUpdate(q, models.M{"days": pt.Days})
var results models.UserSlice
err := q.Bind(ctx, exec, &results) Does this help? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a PG table defined as
CREATE TABLE pt (
c_id int8 NOT NULL,
id varchar(60) NOT NULL,
"uuid" uuid NOT NULL,
days INTEGER,
grace_days INTEGER,
CONSTRAINT pt_pk PRIMARY KEY (c_id, id),
CONSTRAINT pt_uuid_unique UNIQUE (c_id, uuid)
);
ptModel.Update() works beautifully for partial update of 'days' using boil.WhiteList('days') field when WHERE clause is okay to be based on primary key values..
ptModel.Update(ctx,db, boil.Whitelist("days"))
A) How can (if possible) equivalent be accomplished using non primary keys via alternate syntax.. (unique key -- 'uuid' in this case)?
I understand there is UpdateAll, but in that case we need to build the list name, value pairs in the map to pass to the request.. =
B) Can this building of update map be automated by using combination of ptModel and boil.WhiteList("days", "grace_days")
sqlboilermodels.pts(qm.Where("uuid = ?", pt.UUID), qm.Where("c_id = ?", pt.CID)).UpdateAll(ctx, db, sqlboilermodels.M{"days": pt.Days, "grace_days": pt.GraceDays})
Appreciate your response.
Beta Was this translation helpful? Give feedback.
All reactions