Your Question
Adding clause.Returning{} with the return columns specified has no effect does not add a RETURNING clause to the SQL statement.
The document you expected this should be explained
There does not appear to be documentation, nor are there comments in the code that document or provide an example of how to use clause.Returning{}. Therefore it's difficult to tell whether this is a bug or whether it's somehow misconfigured. Regardless, including the clause and passing a Column struct with Table, Name, Alias and Raw populated have no effect: there is no RETURNING clause in the generated SQL and it does not throw an error.
Expected answer
It would be helpful to have a working example. If this is a bug, it seems odd that adding the clause should fail silently.
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days
Comment From: kishankishore29
I am facing the same issue where in the SQL is being generated without the RETURNING clause. Is there an update on this ?
- Code:
server.Database.Model(&person).Clauses(clause.Returning{}).Updates(&person)
- Generated SQL :
UPDATE "people" SET "name"=Test',"age"=10,"email"='test@yahoo.com' WHERE "id" = '54ecef547f6d4f03a6a2e1db4dbe49dc'
Kindly help here.
Comment From: kishankishore29
@siegesmund were you able to figure anything for this one?
Comment From: kishankishore29
@jinzhu any input here ?
Comment From: jinzhu
use latest version and refer https://gorm.io/docs/update.html#Returning-Data-From-Modified-Rows
Comment From: jeremyharisch
I am facing the same issue. @jinzhu I also tried the example from the link you have send, but this results in following SQL:
UPDATE "users" SET "salary"=salary * $1 WHERE role = $2
Is there a current bug?
Comment From: tavakoli1020
Hi, Kindly I have same issue here. I'm using Gorm 1.23.5 and clause.Returning{} dosent generate proper SQL.
var returningUsers []model.User
fields:=[]string{"status"}
updates:= model.User{status: 1}
result:=db.Model(&returningUsers).Clauses(clause.Returning{}).Where("cid = ?", 1).Select(fields).Updates(updates)
Genrated SQL:
UPDATE users SET status=1 WHERE cid = 1
Comment From: vlnd0
Hi guys, I noticed this issue is marked as closed, but it looks like the bug is still around. Can anyone shed some light on the current status? Thanks a bunch
Comment From: aeramu
I just run into this problem. Try to workaround with raw query and I just realized that in MySQL there is no RETURNING
clause. I don't know what's your DB are, but MySQL doesn't support this.
Comment From: vlnd0
@aeramu Hi, I'm using Postgres and it is support returning statement, but I have to use it with Raw() command from Gorm 🥲
Comment From: creator-woow
@vlnd0 Any updates?
Comment From: hongminhcbg
Same here My code
var accs []*models.Account
err := f.DB.WithContext(ctx).Model(&accs).
Clauses(clause.Returning{Columns: []clause.Column{{Name: "name"}, {Name: "salary"}}}).
Where("ledger = ?", 704).Update("debits_posted", gorm.Expr("debits_posted + ?", in.Amount)).Error
return err
the SQL generated
UPDATE `accounts` SET `debits_posted`=debits_posted + 3 WHERE ledger = 704
I'm using go ver 1.23.1 and gorm.io/gorm@v1.25.12