Commit 9f69410a authored by Amos Wenger's avatar Amos Wenger

Fix culling many_to_many assoc disguising as an has_many assoc

parent 27ab15af
Pipeline #11019 passed with stage
in 31 seconds
......@@ -166,6 +166,9 @@ func Test_ManyToManyRevenge(t *testing.T) {
hades.Assoc("Game"),
),
))
numPG, err := c.Count(conn, &ProfileGame{}, builder.NewCond())
wtest.Must(t, err)
assert.EqualValues(t, 3, numPG)
var names []struct {
Name string
......@@ -184,6 +187,17 @@ func Test_ManyToManyRevenge(t *testing.T) {
{"Seconds until midnight"},
{"Three was company"},
}, names)
// delete one
p.ProfileGames = p.ProfileGames[1:]
wtest.Must(t, c.Save(conn, p,
hades.AssocReplace("ProfileGames",
hades.Assoc("Game"),
),
))
numPG, err = c.Count(conn, &ProfileGame{}, builder.NewCond())
wtest.Must(t, err)
assert.EqualValues(t, 2, numPG)
})
}
......
......@@ -151,8 +151,15 @@ func (c *Context) SaveNoTransaction(conn *sqlite.Conn, rec interface{}, opts ...
if vri.Relationship != nil {
switch vri.Relationship.Kind {
case "has_many":
// if we're in replace mode
if vri.Field.Mode() == AssocModeReplace {
cull = true
// and it's an actually
// has_many, not a disguised
// many_to_many
if len(vri.ModelStruct.PrimaryFields) == 1 {
// then cull now
cull = true
}
}
case "many_to_many":
// culling is done later, but let's record the ManyToMany now
......@@ -182,8 +189,9 @@ func (c *Context) SaveNoTransaction(conn *sqlite.Conn, rec interface{}, opts ...
for _, pf := range vri.ModelStruct.PrimaryFields {
pfNames = append(pfNames, pf.Name)
}
return errors.Errorf("Since %v has_many %v, expected %v to have one primary key. Instead, it has primary fields: %s",
pri.Name, vri.Name, strings.Join(pfNames, ", "))
pri.Name(), vri.Name(), vri.Name(), strings.Join(pfNames, ", "))
}
valuePF := c.NewScope(v.Interface()).PrimaryField()
if valuePF == nil {
......
......@@ -83,7 +83,7 @@ func TestExecErr(t *testing.T) {
t.Error(err)
}
err = sqliteutil2.Exec(conn, "INSERT INTO t (c1, c2) VALUES (?, ?);", nil, 1, 1, 1)
if got, want := sqlite.ErrCode(err), sqlite.SQLITE_RANGE; got != want {
if got, want := sqlite.ErrCode(errors.Cause(err)), sqlite.SQLITE_RANGE; got != want {
t.Errorf("INSERT err code=%s, want %s", got, want)
}
......
......@@ -15,10 +15,11 @@
package sqliteutil2
import (
"errors"
"strings"
"testing"
"github.com/pkg/errors"
"crawshaw.io/sqlite"
)
......@@ -143,7 +144,7 @@ func TestDone(t *testing.T) {
relFn := Save(conn)
relFn(&err)
if code := sqlite.ErrCode(err); code != sqlite.SQLITE_INTERRUPT {
if code := sqlite.ErrCode(errors.Cause(err)); code != sqlite.SQLITE_INTERRUPT {
t.Errorf("savepoint release function error code is %v, want SQLITE_INTERRUPT", code)
}
}
......
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