Closed liut closed 1 week ago
We hadn’t previously defined the behavior for when embed and Unique appear together, so I’ve created some test cases.
t.Run("embedWithUnique", func(t *testing.T) {
type Perms struct {
View bool
Create bool
UniqueID int `bun:",unique"`
UniqueGroupID int `bun:",unique:groupa"`
}
type Role struct {
Foo Perms `bun:"embed:foo_"`
Perms
}
table := tables.Get(reflect.TypeOf((*Role)(nil)))
require.Nil(t, table.StructMap["foo"])
require.Nil(t, table.StructMap["bar"])
fooView, ok := table.FieldMap["foo_view"]
require.True(t, ok)
require.Equal(t, []int{0, 0}, fooView.Index)
barView, ok := table.FieldMap["view"]
require.True(t, ok)
require.Equal(t, []int{1, 0}, barView.Index)
require.Equal(t, 3, len(table.Unique))
require.Equal(t, 2, len(table.Unique[""]))
require.Equal(t, "foo_unique_id", table.Unique[""][0].Name)
require.Equal(t, "unique_id", table.Unique[""][1].Name)
require.Equal(t, 1, len(table.Unique["groupa"]))
require.Equal(t, "unique_group_id", table.Unique["groupa"][0].Name)
require.Equal(t, 1, len(table.Unique["foo_groupa"]))
require.Equal(t, "foo_unique_group_id", table.Unique["foo_groupa"][0].Name)
})
In the case where there were many changes in version 1.17 + 1 commit, subsequent versions did not work well with a large number of embeds in my scenario. For this reason, I made minor revisions to make it work. When a unique is detected in the subtable, it is set as the only unique. Generally, Unique tags are not used too much. In my scenario, it just solved the problem.