Skip to content
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

Fix deteministic fetch when table has a composite primary key #936

Merged
merged 1 commit into from Aug 6, 2021

Conversation

wpolicarpo
Copy link
Member

Even though Rails does not support tables with primary keys, it still allows
those tables to be defined without any problems.

The SQL Server adapter will always try to retrieve records in a deterministic
way but it currently fails if the table has a composite primary key, which
shouldn't happen.

This PR fixes how the adapter determines which column (generally the PK) is
used as the default order when multiple primary keys are detected.

It first checks if one of the primary keys is also an identity column. If
there's an identity column, it will choose it as the "main primary key",
otherwise it will just fallback to the first of the primary keys.

Fix #719.

Even though Rails does not support tables with primary keys, it still allows
those tables to be defined without any problems.

The SQL Server adapter will always try to retrieve records in a deterministic
way but it currently fails if the table has a composite primary key, which
shouldn't happen.

This PR fixes how the adapter determines which column (generally the PK) is
used as the default order when multiple primary keys are detected.

It first checks if one of the primary keys is also an identity column. If
there's an identity column, it will choose it as the "main primary key",
otherwise it will just fallback to the first of the primary keys.
@wpolicarpo wpolicarpo merged commit dec6836 into main Aug 6, 2021
@wpolicarpo wpolicarpo deleted the fix-composite-pk-deterministic-fetch branch August 6, 2021 23:07
lavika pushed a commit to lavika/activerecord-sqlserver-adapter that referenced this pull request Sep 26, 2023
…sqlserver#936)

Even though Rails does not support tables with primary keys, it still allows
those tables to be defined without any problems.

The SQL Server adapter will always try to retrieve records in a deterministic
way but it currently fails if the table has a composite primary key, which
shouldn't happen.

This PR fixes how the adapter determines which column (generally the PK) is
used as the default order when multiple primary keys are detected.

It first checks if one of the primary keys is also an identity column. If
there's an identity column, it will choose it as the "main primary key",
otherwise it will just fallback to the first of the primary keys.
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.

primary_Key_From_Table returns composite primary keys which get treated as one key
1 participant