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
ohmayr pushed a commit that referenced this pull request Mar 23, 2026
PR created by the Librarian CLI to initialize a release. Merging this PR
will auto trigger a release.

Librarian Version: v0.0.0-20260216162532-e323d455c92b
Language Image:
us-central1-docker.pkg.dev/cloud-sdk-librarian-prod/images-prod/python-librarian-generator@sha256:160860d189ff1c2f7515638478823712fa5b243e27ccc33a2728669fa1e2ed0c
<details><summary>django-google-spanner: v4.0.3</summary>

##
[v4.0.3](django-google-spanner-v4.0.2...django-google-spanner-v4.0.3)
(2026-03-23)

### Bug Fixes

* db_returning should only be overridden for Spanner (#16111)
([8c1eb13](8c1eb13f))

</details>


<details><summary>google-cloud-dataplex: v2.17.0</summary>

##
[v2.17.0](google-cloud-dataplex-v2.16.0...google-cloud-dataplex-v2.17.0)
(2026-03-23)

### Features

* add support for attaching aspects to EntryLinks (PiperOrigin-RevId:
883306841)
([ee7dd7d](ee7dd7dc))

* add DataProductService to manage data products and underlying data
assets (PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

* add LookupContext to CatalogService for LLM-generated resource context
(PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

* add debug query support to Data Quality rules (PiperOrigin-RevId:
883306841)
([ee7dd7d](ee7dd7dc))

* add UpdateEntryLink and LookupEntryLinks methods to CatalogService
(PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

* allow selective generation scope for Data Documentation scans
(PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

* support OneTime triggers for DataScan operations (PiperOrigin-RevId:
883306841)
([ee7dd7d](ee7dd7dc))

* add SKIPPED state to DataScan catalog publishing status
(PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

* add MetadataFeed to CatalogService for tracking metadata changes
(PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

### Bug Fixes

* remove deprecated Explore-related methods and messages from public
client libraries Breaking Changes: - Removed ContentService and all
associated methods (CreateContent, UpdateContent, DeleteContent,
GetContent, ListContent, etc.) and request/response messages. - Removed
Environment and Session management methods from DataplexService
(CreateEnvironment, UpdateEnvironment, DeleteEnvironment,
ListEnvironments, GetEnvironment, ListSessions) and their associated
messages. (PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

### Documentation

* remove deprecated metadata change warnings in Dataplex Catalog The
DataProductService provides APIs to curate and manage collections of
data assets as data products, enabling more organized sharing and usage
for specific business cases. MetadataFeeds allow users to monitor
metadata changes (CREATE, UPDATE, DELETE) within a specified scope
(organization, project, or entry group) and publish them to Pub/Sub.
CatalogService now includes a LookupContext API to provide LLM-generated
context for resources, and enhanced EntryLink management, including the
ability to attach aspects. DataScan operations now support a OneTime
trigger for single-run scans, and Data Quality rules support
DebugQueries to help investigate rule failures by returning diagnostic
values. (PiperOrigin-RevId: 883306841)
([ee7dd7d](ee7dd7dc))

</details>


<details><summary>google-cloud-documentai: v3.12.0</summary>

##
[v3.12.0](google-cloud-documentai-v3.11.0...google-cloud-documentai-v3.12.0)
(2026-03-23)

### Features

* Add a field for upgrading previous processor version when fine tuning
(PiperOrigin-RevId: 882129422)
([ee7dd7d](ee7dd7dc))

</details>


<details><summary>google-cloud-firestore: v2.26.0</summary>

##
[v2.26.0](google-cloud-firestore-v2.25.0...google-cloud-firestore-v2.26.0)
(2026-03-23)

### Features

* Add `Rand` and `Trunc` expressions (#16037)
([7538aa5](7538aa5d))

</details>


<details><summary>google-cloud-iap: v1.20.0</summary>

##
[v1.20.0](google-cloud-iap-v1.19.0...google-cloud-iap-v1.20.0)
(2026-03-23)

### Features

* add oauth fields for IapSettings (PiperOrigin-RevId: 883439941)
([ee7dd7d](ee7dd7dc))

</details>


<details><summary>google-cloud-network-connectivity: v2.14.0</summary>

##
[v2.14.0](google-cloud-network-connectivity-v2.13.0...google-cloud-network-connectivity-v2.14.0)
(2026-03-23)

### Features

* onboard a new library (PiperOrigin-RevId: 876282040)
([2647414](2647414d))

### Bug Fixes

* add warehouse package name to v1beta (PiperOrigin-RevId: 885295553)
([3701721](37017215))

</details>


<details><summary>google-cloud-securitycenter: v1.43.0</summary>

##
[v1.43.0](google-cloud-securitycenter-v1.42.0...google-cloud-securitycenter-v1.43.0)
(2026-03-23)

### Features

* Support Chokepoint and external exposure in findings Proto
(PiperOrigin-RevId: 884685891)
([ee7dd7d](ee7dd7dc))

</details>


<details><summary>google-cloud-vectorsearch: v0.8.0</summary>

##
[v0.8.0](google-cloud-vectorsearch-v0.7.0...google-cloud-vectorsearch-v0.8.0)
(2026-03-23)

### Features

* Add support for ExportDataObjects (PiperOrigin-RevId: 882214457)
([ee7dd7d](ee7dd7dc))

* Mark Vector Search v1 API as GA (PiperOrigin-RevId: 882214457)
([ee7dd7d](ee7dd7dc))

### Documentation

* Update changelog entry for release (#16092)
([1289664](12896643))

</details>


<details><summary>google-maps-navconnect: v0.1.0</summary>

##
[v0.1.0](google-maps-navconnect-v0.0.0...google-maps-navconnect-v0.1.0)
(2026-03-23)

### Features

* onboard a new library (PiperOrigin-RevId: 879267201)
([2158300](21583003))

</details>


<details><summary>google-shopping-css: v0.4.0</summary>

##
[v0.4.0](google-shopping-css-v0.3.0...google-shopping-css-v0.4.0)
(2026-03-23)

### Features

* add product rating fields to CSS API v1. This is in preparation for an
upcoming feature and the new fields are not yet used.
(PiperOrigin-RevId: 882470295)
([ee7dd7d](ee7dd7dc))

### Documentation

* A comment for field `name` in message
`.google.shopping.css.v1.GetCssProductRequest` is changed
(PiperOrigin-RevId: 882470295)
([ee7dd7d](ee7dd7dc))

* A comment for field `size_types` in message
`.google.shopping.css.v1.Attributes` is changed (PiperOrigin-RevId:
882470295)
([ee7dd7d](ee7dd7dc))

* A comment for field `name` in message
`.google.shopping.css.v1.CssProductInput` is changed (PiperOrigin-RevId:
882470295)
([ee7dd7d](ee7dd7dc))

* A comment for field `name` in message
`.google.shopping.css.v1.AccountLabel` is changed (PiperOrigin-RevId:
882470295)
([ee7dd7d](ee7dd7dc))

</details>


<details><summary>google-shopping-merchant-accounts: v1.4.0</summary>

##
[v1.4.0](google-shopping-merchant-accounts-v1.3.0...google-shopping-merchant-accounts-v1.4.0)
(2026-03-23)

### Features

* Add CreateTestAccount RPC to Accounts service for creating test-only
Merchant Center accounts (PiperOrigin-RevId: 884882604)
([75697a6](75697a6f))

### Documentation

* Improve TermsOfServiceKind.MERCHANT_CENTER comment (PiperOrigin-RevId:
884882604)
([75697a6](75697a6f))

* Update Region to support radius_area and clarify area type exclusivity
(PiperOrigin-RevId: 884882604)
([75697a6](75697a6f))

* Update Account.account_name documentation for naming restrictions
(PiperOrigin-RevId: 884882604)
([75697a6](75697a6f))

* Clarify CheckoutSettings.eligible_destinations usage
(PiperOrigin-RevId: 884882604)
([75697a6](75697a6f))

* Add product-ratings to Program documentation (PiperOrigin-RevId:
884882604)
([75697a6](75697a6f))

* Update DeleteUser method description (PiperOrigin-RevId: 884882604)
([75697a6](75697a6f))

* Refine DeveloperRegistration.developer_email behavior description
(PiperOrigin-RevId: 884882604)
([75697a6](75697a6f))

</details>


<details><summary>google-shopping-merchant-products: v1.4.0</summary>

##
[v1.4.0](google-shopping-merchant-products-v1.3.0...google-shopping-merchant-products-v1.4.0)
(2026-03-23)

### Features

* Added several fields to enhance shipping configurations: -
`handling_cutoff_time` and `handling_cutoff_timezone` within the
`Shipping` message - `ShippingBusinessDaysConfig` message to define
business days for shipping - `shipping_handling_business_days` and
`shipping_transit_business_days` in `ProductAttributes` -
`HandlingCutoffTime` message to configure country-specific handling
cutoffs - `handling_cutoff_times` array in `ProductAttributes`
(PiperOrigin-RevId: 830818193)
([ee7dd7d](ee7dd7dc))

* Added C#, PHP, and Ruby namespace options to ProductInputs, Products,
and ProductsCommon proto files for improved client library generation
(PiperOrigin-RevId: 830818171)
([ee7dd7d](ee7dd7dc))

* update products_common fields to include `handling_cutoff_timezone `,
`shipping_handling_business_days`, `shipping_transit_business_days`
(PiperOrigin-RevId: 881874426)
([ee7dd7d](ee7dd7dc))

* Added `handling_cutoff_time` and `handling_cutoff_timezone` fields to
the `Shipping` message within `Attributes` (PiperOrigin-RevId:
830818171)
([ee7dd7d](ee7dd7dc))

* Added the `product_id_base64_url_encoded` field to
`InsertProductInputRequest`, `DeleteProductInputRequest`, and
`GetProductRequest`. This allows for product IDs containing special
characters to be correctly handled when unpadded base64url-encoded
(PiperOrigin-RevId: 830818193)
([ee7dd7d](ee7dd7dc))

### Documentation

* Updated various comments, including links to data source creation
guides (PiperOrigin-RevId: 830818193)
([ee7dd7d](ee7dd7dc))

* Updated comments for several fields, including product name formats,
data source creation, destination field descriptions (now also referred
to as Marketing Methods), and the default page size for
`ListProductsRequest` (PiperOrigin-RevId: 830818171)
([ee7dd7d](ee7dd7dc))

</details>


<details><summary>grafeas: v1.21.0</summary>

##
[v1.21.0](grafeas-v1.20.0...grafeas-v1.21.0)
(2026-03-23)

### Features

* Added line_number to FileLocation (PiperOrigin-RevId: 882149723)
([ee7dd7d](ee7dd7dc))

</details>


<details><summary>sqlalchemy-spanner: v1.17.3</summary>

##
[v1.17.3](sqlalchemy-spanner-v1.17.2...sqlalchemy-spanner-v1.17.3)
(2026-03-23)

### Bug Fixes

* provide default sqlite db config for unit tests
([03cce70](03cce701))

* add missing docs and docfx sessions
([8fb857d](8fb857df))

* remove test_config generation from unit session
([9cc1f90](9cc1f90b))

</details>
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