Home > Backend Development > Golang > golang gorm update and return

golang gorm update and return

PHPz
Release: 2024-02-05 22:06:12
forward
596 people have browsed it

golang gorm 更新并返回

Question content

In my golang project, I use gorm, I need to make an upsert query using return clause to get the modifications from the query value after. I can do an upsert, but I don't know how to connect the return clause to it. The table name is counters, and the code is as follows:

te := struct {
        name string //key column
        counter int
    }{
        name: "name_to_update",
        counter: 2,
    }

db.
    //model(te).
    clauses(
        //clause.returning{columns: []clause.column{{name: "counter"}}},
        clause.onconflict{
            columns: []clause.column{{name: "name"}}, // key column
            doupdates: clause.assignments(map[string]interface{}{
                "counter": gorm.expr("counters.counter + ?", 1),
            }),
        },
    ).create(&te)
Copy after login

The generated sql query is:

INSERT INTO "counters" ("counter", "name") VALUES (0, "name_to_update") ON CONFLICT ("name") 
DO UPDATE SET "counter"=counters.counter + 1 RETURNING "name" //I need updated counter value here, not name
Copy after login

So the counter is updated, which is no problem, but when I need the updated value of the counter, it returns the key column (in the return). Any ideas how to fix it? Thanks


Correct answer


I'm not sure if the anonymous structure is causing the problem.

Also, it's not clear from your code where the table name - "counters" - comes from.

I've tried your solution - but using a dedicated structure for the model - and it works great.

type counter struct {
    name    string `gorm:"primarykey"`
    counter int
}

...

counter := counter{name: "name_to_update", counter: 2}

    db.
        clauses(
            clause.returning{columns: []clause.column{{name: "counter"}}},
            clause.onconflict{
                columns: []clause.column{{name: "name"}},
                doupdates: clause.assignments(map[string]interface{}{
                    "counter": gorm.expr("counters.counter + ?", 1),
                }),
            },
        ).create(&counter)

    fmt.println(counter.counter)
Copy after login

The above code generates the following sql

INSERT INTO "counters" ("name","counter") VALUES ('name_to_update',10) ON CONFLICT ("name") DO UPDATE SET "counter"=counters.counter + 1 RETURNING "counter"
Copy after login

And counter.counter has the correct updated value.

The above is the detailed content of golang gorm update and return. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template