When using publish2.version with a has many relationship (e.g., Factory has many Items), you must ensure the 'many' side (Item) includes the resource.CompositePrimaryKeyField.
To implement a remote selector that supports composite primary keys, you must configure an ID meta field using a Valuer that calls resource.GenCompositePrimaryKey(r.ID, r.GetVersionName()). Finally, apply this to the parent resource using admin.SelectManyConfig.
// 1. Define models with CompositePrimaryKeyField on the 'many' side
type Factory struct {
gorm.Model
Name string
publish2.Version
Items []Item `gorm:"many2many:factory_items;association_autoupdate:false"`
ItemsSorter sorting.SortableCollection
}
type Item struct {
gorm.Model
Name string
publish2.Version
// github.com/qor/qor/resource
resource.CompositePrimaryKeyField // Required
}
// 2. Define a remote resource selector with a custom ID Valuer
func generateRemoteItemSelector(adm *admin.Admin) (res *admin.Resource) {
res = adm.AddResource(&Item{}, &admin.Config{Name: "ItemSelector"})
res.IndexAttrs("ID", "Name")
res.Meta(&admin.Meta{
Name: "ID",
Valuer: func(value interface{}, ctx *qor.Context) interface{} {
if r, ok := value.(*Item); ok {
return resource.GenCompositePrimaryKey(r.ID, r.GetVersionName())
}
return ""
},
})
return res
}
// 3. Use it in the Factory resource
itemSelector := generateRemoteItemSelector(adm)
factoryRes.Meta(&admin.Meta{
Name: "Items",
Config: &admin.SelectManyConfig{
RemoteDataResource: itemSelector,
},
})