-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(spanner-django): db_returning should only be overridden for Spanner #16111
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -408,19 +408,27 @@ def test_autofield_no_default(self): | |
| """Spanner, default is not provided.""" | ||
| 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 | ||
|
|
||
| def test_autofield_default(self): | ||
| """Spanner, default provided.""" | ||
| mock_func = mock.Mock() | ||
| field = AutoField(name="field_name", default=mock_func) | ||
| assert gen_rand_int64 != field.default | ||
| assert mock_func == field.default | ||
| # A default was already provided, so Spanner does not generate random IDs client-side. | ||
| # Therefore, db_returning does not need to be overridden to False. | ||
| assert field.db_returning is True | ||
|
|
||
| def test_autofield_not_spanner(self): | ||
| """Not Spanner, default not provided.""" | ||
| connection.settings_dict["ENGINE"] = "another_db" | ||
| field = AutoField(name="field_name") | ||
| assert gen_rand_int64 != field.default | ||
| # db_returning should remain untouched (implicitly True) for non-Spanner databases | ||
| # so that Django retrieves the auto-increment ID correctly. | ||
| assert field.db_returning is True | ||
| connection.settings_dict["ENGINE"] = "django_spanner" | ||
|
|
||
| def test_autofield_not_spanner_w_default(self): | ||
|
|
@@ -430,6 +438,8 @@ def test_autofield_not_spanner_w_default(self): | |
| field = AutoField(name="field_name", default=mock_func) | ||
| assert gen_rand_int64 != field.default | ||
| assert mock_func == field.default | ||
| # Because it's not a Spanner database, the behavior shouldn't be altered in any way. | ||
| assert field.db_returning is True | ||
| connection.settings_dict["ENGINE"] = "django_spanner" | ||
|
|
||
| def test_autofield_spanner_as_non_default_db_random_generation_enabled( | ||
|
|
@@ -441,6 +451,9 @@ def test_autofield_spanner_as_non_default_db_random_generation_enabled( | |
| connections.settings["secondary"]["RANDOM_ID_GENERATION_ENABLED"] = "true" | ||
| field = AutoField(name="field_name") | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| connections.settings["default"]["ENGINE"] = "django_spanner" | ||
| connections.settings["secondary"]["ENGINE"] = "django_spanner" | ||
| del connections.settings["secondary"]["RANDOM_ID_GENERATION_ENABLED"] | ||
|
|
@@ -450,4 +463,7 @@ def test_autofield_random_generation_disabled(self): | |
| connections.settings["default"]["RANDOM_ID_GENERATION_ENABLED"] = "false" | ||
| field = AutoField(name="field_name") | ||
| assert gen_rand_int64 != field.default | ||
| # Because we're delegating ID generation back to the database backend, | ||
| # Django needs to be able to retrieve the assigned ID. | ||
| assert field.db_returning is True | ||
| del connections.settings["default"]["RANDOM_ID_GENERATION_ENABLED"] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency and clarity, it's better to access the
db_returningattribute directly. TheAutoFieldclass defines this attribute, so it will always be present on thefieldinstance. Usinggetattrwith a default is unnecessary and makes the assertion slightly harder to read compared to a direct access.