Skip to content

Composite Primary Key without autoIncrement fails in FindInBatches #800

@keif888

Description

@keif888

GORM Playground Link

#799

Description

Setup

Create a struct with 2 (or more) columns in the primary key, none of which has autoIncrement set to true.
Populate an array of the struct with more records than the batch size
Call create to populate database table
Use FindInBatches to retrieve the records just inserted

Expected

FindInBatches is able to retrieve all the records in batches

Actual

FindInBatches fails with error primary key required

Fixing this

A change to gorm/schema/schema.go to set the PrioritizedPrimaryField when there are more than 1 column in the primary key as per below is not enough, as it will not retrieve all records in the pull above.

Snippet starting line 269:

 		if len(schema.PrimaryFields) == 1 {
			schema.PrioritizedPrimaryField = schema.PrimaryFields[0]
		} else if len(schema.PrimaryFields) > 1 {
+			// Set default primary key field in case there are no autoincrement fields in composite pk.
+			schema.PrioritizedPrimaryField = schema.PrimaryFields[0]
			// If there are multiple primary keys, the AUTOINCREMENT field is prioritized
			for _, field := range schema.PrimaryFields {
				if field.AutoIncrement {

The ability to order by the entire set of primary key fields, and have the set of primary key fields included in the where clause of the 2nd and beyond queries in FindInBatches is required.

For the pull the following queries need to be generated:

[rows:9] INSERT INTO `categories` (`label_id`,`category_id`) VALUES (1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)
[rows:5] SELECT * FROM `categories` ORDER BY `categories`.`label_id`, `categories`.`category_id` LIMIT 5
[rows:4] SELECT * FROM `categories` WHERE (`categories`.`label_id` = 2 AND `categories`.`category_id` > 2) OR `categories`.`label_id` > 2 ORDER BY `categories`.`label_id`, `categories`.`category_id` LIMIT 5

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions