Skip to content

fix(spanner-django): db_returning should only be overridden for Spanner#16111

Merged
olavloite merged 1 commit intomainfrom
issue-15930
Mar 16, 2026
Merged

fix(spanner-django): db_returning should only be overridden for Spanner#16111
olavloite merged 1 commit intomainfrom
issue-15930

Conversation

@olavloite
Copy link
Contributor

The db_returning attribute of AutoFields should only be overridden for Spanner database connections, and not globally for all databases.

Note: Many tests are failing. These failures are not due to these changes, but due to existing failures for this library.

Fixes #15930

Replaces googleapis/python-spanner-django#961

The db_returning attribute of AutoFields should only be overridden for Spanner database connections,
and not globally for all databases.

Note: Many tests are failing. These failures are not due to these changes, but due to existing
failures for this library.

Fixes #15930
@olavloite olavloite requested review from a team as code owners March 16, 2026 11:00
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where Django's AutoField behavior was being incorrectly modified globally, impacting database connections beyond Spanner. The changes ensure that the db_returning attribute, which dictates how Django retrieves auto-generated IDs, is only overridden for Spanner connections when client-side ID generation is active. This correction prevents unintended side effects on other database backends and improves the robustness of the django-google-spanner library.

Highlights

  • Conditional db_returning override: The db_returning attribute for AutoField is now conditionally set to False only when a Spanner database connection is active and client-side ID generation is enabled, ensuring correct behavior for other database types.
  • Removed global overrides: Global overrides for AutoField.db_returning and AutoField.validators were removed, preventing unintended side effects on non-Spanner database connections.
  • Enhanced test coverage: New assertions were added to unit tests to validate the correct behavior of db_returning under various Spanner and non-Spanner database configurations, including cases with and without default values and explicit random ID generation settings.
Changelog
  • packages/django-google-spanner/django_spanner/init.py
    • Removed global db_returning and validators overrides for AutoField, SmallAutoField, and BigAutoField.
    • Moved self.db_returning = False and self.validators = [] into the autofield_init method, making them conditional on the Spanner RANDOM_ID_GENERATION_ENABLED setting.
  • packages/django-google-spanner/tests/unit/django_spanner/test_schema.py
    • Added assertions to test_autofield_no_default to verify db_returning is False when Spanner handles ID generation.
    • Added assertions to test_autofield_default to verify db_returning is True when a default is provided.
    • Added assertions to test_autofield_not_spanner and test_autofield_not_spanner_w_default to verify db_returning remains True for non-Spanner databases.
    • Added assertions to test_autofield_spanner_as_non_default_db_random_generation_enabled to verify db_returning is False when client-side random generation is enabled for a specific connection.
    • Added assertions to test_autofield_random_generation_disabled to verify db_returning is True when ID generation is delegated to the database.
Activity
  • No specific activity (comments, reviews, progress) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly refactors the monkey-patching of Django's AutoField to be conditional, applying Spanner-specific behavior only when a Spanner database is configured for client-side ID generation. This is a good improvement as it avoids globally altering AutoField behavior, which could impact other database backends in a multi-DB setup. The logic change in django_spanner/__init__.py is sound, and the new tests in tests/unit/django_spanner/test_schema.py provide good coverage. The minor suggestions to improve the readability and consistency of the test assertions have been retained.

field = AutoField(name="field_name")
assert gen_rand_int64 == field.default
# db_returning must be explicitly False because Spanner is handling ID generation client-side
assert getattr(field, "db_returning", True) is False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency and clarity, it's better to access the db_returning attribute directly. The AutoField class defines this attribute, so it will always be present on the field instance. Using getattr with a default is unnecessary and makes the assertion slightly harder to read compared to a direct access.

Suggested change
assert getattr(field, "db_returning", True) is False
assert field.db_returning is False

assert gen_rand_int64 == field.default
# Since this specific connection explicitly enables client-side random generation,
# we must tell Django not to attempt retrieving the DB's returned ID.
assert getattr(field, "db_returning", True) is False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to my other comment, direct attribute access is preferred here for better readability and consistency. Since db_returning is a standard attribute of AutoField, getattr is not needed.

Suggested change
assert getattr(field, "db_returning", True) is False
assert field.db_returning is False

Copy link
Contributor

@chalmerlowe chalmerlowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@olavloite olavloite merged commit 8c1eb13 into main Mar 16, 2026
51 of 56 checks passed
@olavloite olavloite deleted the issue-15930 branch March 16, 2026 15:05
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.

Auto-increment ID from MySQL is not populated in model instance when Spanner is the default DB

3 participants