fix(spanner-django): db_returning should only be overridden for Spanner#16111
fix(spanner-django): db_returning should only be overridden for Spanner#16111
Conversation
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
Summary of ChangesHello, 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 Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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