-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Support Postgres new upsert feature 'ON CONFLICT' #721
Comments
In the interest of bumping this item, it should be noted that Postgres 9.5 was released today. |
Is this supported on MySQL for |
Is the feature postgresql upsert feature supported? If yes, can we get examples? |
The feature is technically supported, but not in a great way. The lines from @jinzhu's change, jinzhu@c9dfd80#diff-04c6e90faac2675aa89e2176d2eec7d8R265, are true in that the sql produced is correct, but you'd need to extend "ON CONFLICT" there to specify the behavior you wanted, such as I needed upserts, and ended up writing the insert sql myself and using Exec. It also means batch upsert is out of the question; I have it running in a loop for each record. |
I wasn't sure Postgres supported upserting multiple rows at once, so I looked into it. Looks like you can use the db.Set("gorm:insert_option", "ON CONFLICT (name) DO UPDATE SET code = EXCLUDED.code").Create(&products)
// INSERT INTO products (name, code)
// VALUES
// ('name', 'code'),
// ('another name', 'another code')
// ON CONFLICT (name) DO UPDATE SET
// code = EXCLUDED.code; |
@calebthompson, @jinzhu using your example, I attempted both upserting multiple rows at once, as well as one at a time and executing raw SQL. Attempting to upsert multiple at once looked like the following:
This results in the following error:
The attempt at upserting single record at a time looks like the following:
This results in the following error
Any ideas what could be causing this, or how I can fix it? I would prefer to be able to accomplish upsert of multiple rows. |
@jinzhu, any guidance here? |
Any updates? If |
? |
|
For the Set Values: |
var fields []string
for _, field := range tx.NewScope(Session{}).Fields() {
if field.Name != "AccountID" && field.Name != "InvoiceID" {
if field.IsNormal && !field.IsIgnored {
fields = append(fields, fmt.Sprintf(
"%v = excluded.%v", field.DBName, field.DBName,
))
}
}
}
var session = &Session{
ID: utils.EnsureUUID(s.Get("sessionUUID")),
RemoteAddr: &r.RemoteAddr,
UserAgent: utils.StringPtr(r.UserAgent()),
Referer: utils.StringPtr(r.Referer()),
Method: &r.Method,
URL: utils.StringPtr(r.URL.String()),
}
tx.Preload("Account", "Invoice").Set(
"gorm:insert_option", fmt.Sprintf(
"ON CONFLICT (id) DO UPDATE SET %s", strings.Join(fields, ", "),
),
).Create(session)
s.Set("sessionUUID", session.ID) |
@KellyLSB Nice trick and this will do for now, but that'd be nice to have this feature in the ORM directly |
www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT
The text was updated successfully, but these errors were encountered: