Commit 918412f4 authored by Amos Wenger's avatar Amos Wenger

Avoid violating primary key constraints when we're saving duplicate records...

Avoid violating primary key constraints when we're saving duplicate records with composite primary keys
parent 6eb4e36c
Pipeline #11178 passed with stage
in 1 minute and 22 seconds
......@@ -39,3 +39,22 @@ func Test_CompositePrimaryKey(t *testing.T) {
assert.EqualValues(t, dataCount, 1)
})
}
func Test_SaveDuplicateCompositePrimaryKeys(t *testing.T) {
type Helicopter struct {
A int64 `hades:"primary_key"`
B string `hades:"primary_key"`
}
models := []interface{}{
&Helicopter{},
}
withContext(t, models, func(conn *sqlite.Conn, c *hades.Context) {
hh := []*Helicopter{
&Helicopter{A: 1, B: "hey"},
&Helicopter{A: 1, B: "hey"},
}
wtest.Must(t, c.Save(conn, hh))
})
}
\ No newline at end of file
......@@ -38,7 +38,7 @@ func (c *Context) saveJoins(conn *sqlite.Conn, mode AssocMode, mtm *ManyToMany)
var deletes []interface{}
updates := make(map[interface{}]ChangedFields)
var inserts []JoinRec
insertsByDestinKey := make(map[interface{}]JoinRec)
// compare with cache: will result in delete or update
for i := 0; i < cache.Len(); i++ {
......@@ -66,7 +66,7 @@ func (c *Context) saveJoins(conn *sqlite.Conn, mode AssocMode, mtm *ManyToMany)
for _, joinRec := range joinRecs {
if _, ok := cacheByDestinKey[joinRec.DestinKey]; !ok {
inserts = append(inserts, joinRec)
insertsByDestinKey[joinRec.DestinKey] = joinRec
}
}
......@@ -77,7 +77,7 @@ func (c *Context) saveJoins(conn *sqlite.Conn, mode AssocMode, mtm *ManyToMany)
}
}
for _, joinRec := range inserts {
for _, joinRec := range insertsByDestinKey {
rec := joinRec.Record
if rec.IsValid() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment