-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Open
Labels
type:with reproduction stepswith reproduction stepswith reproduction steps
Description
GORM Playground Link
FindInBatches reports "primary key required" due to this bug.
This ticket is based on a reported issue by @fanlvG from Jul 8, 2022 (#5495). This issue still exists on the latest version of GORM (tested on v1.31.0).
FindInBatches works depends on sorting primaryKey. when model has more primaryKeys, FindInBatches should use first primaryKeys as sort condition. reference: https://gorm.io/docs/write_plugins.html
while in schema/schema.go:217. set PrioritizedPrimaryField has bug.
if schema.PrioritizedPrimaryField == nil && len(schema.PrimaryFields) == 1 {
schema.PrioritizedPrimaryField = schema.PrimaryFields[0]
}
in gorm.io docs. PrioritizedPrimaryField described as:
// Prioritized primary key field: field with DB name
idor the first defined primary key
in "Write Plugins" chapter.
please check this out.
correct code should be like:
if schema.PrioritizedPrimaryField == nil && len(schema.PrimaryFields) >= 1 {
schema.PrioritizedPrimaryField = schema.PrimaryFields[0]
}
Metadata
Metadata
Assignees
Labels
type:with reproduction stepswith reproduction stepswith reproduction steps