v10 Bulk Insert With OnConflict Removes Elements From Passed Slice
Perplex opened this issue · 4 comments
Perplex commented
When bulk inserting a slice, with OnConflict("DO NOTHING"), and some or all elements already exist in the DB. The resulting slice after the bulk insert is missing elements.
Expected Behavior
Slice contains same number of elements before and after bulk insert.
Current Behavior
Slice contains less elements after bulk insert.
Steps to Reproduce
type model struct {
ID int
}
// Assume model-2 already exists in DB
models = []*model{
{
ID: 1,
},
{
ID: 2,
}
}
// slice contain's two elements at this point
if _, err := db.Model(&models).OnConflict("DO NOTHING").Insert(); err != nil {
....
}
// slice contain's only one element, model-1
Context (Environment)
- go-pg V10
- Windows 10
vmihailenco commented
You need to add Returning("NULL")
if you don't want to scan returned rows.
Perplex commented
This will return all the elements back but now IDs aren't populated in the slice. Im guessing there isn't a way to have both?
vmihailenco commented
Nope, except you could save the original slice and merge it with the result returned from db.
Perplex commented
Decided to just fetch the all inserted rows in a follow up query, thankfully its a small set.