8000 Composite pk handling by keif888 · Pull Request #7453 · go-gorm/gorm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Composite pk handling #7453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Conversation

keif888
Copy link
@keif888 keif888 commented May 12, 2025
  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

Ensure that Composite Primary Keys are handled correctly.
Fix issue go-gorm/playground#800
Add new test for FindInBatch with Composite Primary key

User Case Description

When using a struct with a composite primary key, all fields in the primary key should be used in the order by's to ensure that all databases will guarantee returning the same record(s) in the same order every time.
This is applicable to First, Last, FirstOrCreate, FirstOrInit and FindInBatch.

When getting subsequent batches in FindInBatch, the where clause has to take into account all parts of the composite primary key.

First, FirstOrCreate, FirstOrInit Use Case

  1. create struct with multiple columns tagged with primaryKey
  2. load struct with many rows of data, with the 1st column in the primary key repeated many times
  3. issue a First/FirstOrCreate/FirstOrInit command

Expected:
The same record is always returned.
Actual:
At a random, usually under heavy load, the dbms may return a different 1st record, although it has the same value for the 1st column in the primary key.
This Pull:
Guaranteed to return the same record every time as all columns in the primary key are included in the order by statement.

Last Use Case

  1. create struct with multiple columns tagged with primaryKey
  2. load struct with many rows of data, with the 1st column in the primary key repeated many times
  3. issue a Last command

Expected:
The same record is always returned.
Actual:
At a random, usually under heavy load, the dbms may return a different record, although it has the same 1st column in the primary key.
The record returned may not be the last record when taking all columns in the composite primary key into account, as the order by does not specify all the columns.
This Pull:
Guaranteed to return the same record every time as all columns in the primary key are included in the order by statement.

FindInBatch Use Case

  1. create struct with multiple columns tagged with primaryKey
  2. load struct with many rows of data, with the 1st column in the primary key repeated many times
  3. issue a FindInBatch command with a batch smaller than the number of records
  4. process the batches until all records consumed

Expected:
All the records are returned in batches
Actual:
A no primary key error is returned
This Pull:
1st issuance for FindInBatch has all columns in primary key in the order by.
Subsequent batches have where clause to ensure that the already consumed batches are excluded, and remaining records are returned up to the batch limit.

@keif888
Copy link
Author
keif888 commented May 12, 2025

FYI:
The test ExampleLRU appears to be unstable.
It is not utilising any of the code that this Pull changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0