Title: Auto-increment ID from MySQL is not populated in model instance when Spanner is the default DB · Issue #15930 · googleapis/google-cloud-python · GitHub
Open Graph Title: Auto-increment ID from MySQL is not populated in model instance when Spanner is the default DB · Issue #15930 · googleapis/google-cloud-python
X Title: Auto-increment ID from MySQL is not populated in model instance when Spanner is the default DB · Issue #15930 · googleapis/google-cloud-python
Description: Summary When using a multi-database setup with django-google-spanner as the default database and Cloud SQL (MySQL) as a secondary database, the auto-incremented ID from MySQL is not being populated back into the model instance after savi...
Open Graph Description: Summary When using a multi-database setup with django-google-spanner as the default database and Cloud SQL (MySQL) as a secondary database, the auto-incremented ID from MySQL is not being populated...
X Description: Summary When using a multi-database setup with django-google-spanner as the default database and Cloud SQL (MySQL) as a secondary database, the auto-incremented ID from MySQL is not being populated...
Opengraph URL: https://github.com/googleapis/google-cloud-python/issues/15930
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Auto-increment ID from MySQL is not populated in model instance when Spanner is the default DB","articleBody":"### Summary\n\nWhen using a multi-database setup with `django-google-spanner` as the `default` database and Cloud SQL (MySQL) as a secondary database, the auto-incremented ID from MySQL is not being populated back into the model instance after saving.\n\n### Environment\n\n* **django-google-spanner version:** 4.0.1 (also tested with 4.0.2)\n* **Django version:** 4.2\n* **Database Backend (Default):** `django_spanner`\n* **Database Backend (Secondary):** `mysql.connector.django`\n\n### Problem Description\n\nWe have a multi-database configuration as follows:\n\n**[1] settings.py**\n```python\nDATABASES = {\n 'default': {\n 'ENGINE': 'django_spanner',\n 'PROJECT': GCP_PROJECT_ID,\n 'INSTANCE': SPANNER_INSTANCE,\n 'NAME': SPANNER_DATABASE,\n },\n 'cloudsql': {\n 'ENGINE': 'mysql.connector.django',\n 'NAME': SQL_DATABASE,\n 'USER': USER,\n 'PASSWORD': PASSWORD,\n 'HOST': HOST,\n 'OPTIONS': {'charset': 'utf8mb4', 'collation': 'utf8mb4_bin'},\n }\n}\n```\n\nWe have a model that is configured to use the cloudsql (MySQL) database. The primary key is defined with BigAutoField.\n\n[2] models.py\n\n```python\nclass DeviceMngModel(models.Model):\n # This model is routed to the 'cloudsql' database.\n device_mng_id = models.BigAutoField(primary_key=True)\n # ... other fields\n```\n\nScenario 1: With django-google-spanner==4.0.1 (Default settings)\n\nWhen we create a new record for this model, the device_mng_id is saved to the MySQL table as a very large random integer (likely a 64-bit random number), instead of a standard auto-incremented value.\n\nScenario 2: With django-google-spanner==4.0.2 and a setting change\n\nBased on the library's features, we tried upgrading to 4.0.2 and adding 'RANDOM_ID_GENERATION_ENABLED': 'false' to the default database configuration.\n```\n# settings.py addition\n'default': {\n 'ENGINE': 'django_spanner',\n 'PROJECT': GCP_PROJECT_ID,\n 'INSTANCE': SPANNER_INSTANCE,\n 'NAME': SPANNER_DATABASE,\n 'RANDOM_ID_GENERATION_ENABLED': 'false',\n},\n```\nWith this change, the record in the MySQL table is correctly saved with a proper auto-incremented ID (e.g., 1, 2, 3...).\n\nHowever, the ID is not returned to the Django model instance after the save() method is called. This means we cannot access the newly created ID from the serializer or the model instance.\n\n[3] Code flow\n\n```python\n\nserializer = self.get_serializer(data=data)\nserializer.save()\n# After save(), the ID is not available here:\nprint(serializer.data['device_mng_id'])\n```\n\nExpected Behavior\nAfter serializer.save() is called, the auto-generated device_mng_id from the MySQL database should be populated in the model instance (instance.device_mng_id) and be available in serializer.data.\n\nActual Behavior\nWith 'RANDOM_ID_GENERATION_ENABLED': 'false', the device_mng_id is None on the model instance after saving, and the ID field is absent from serializer.data.\n\nIt seems that the `django-google-spanner` backend, being the default, might be interfering with the mechanism Django uses to fetch the last inserted ID from non-default MySQL databases.\n\nCould you please advise on a possible solution or workaround for this issue? Thank you.","author":{"url":"https://github.com/miyamoto-kei","@type":"Person","name":"miyamoto-kei"},"datePublished":"2025-07-24T00:03:42.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/15930/google-cloud-python/issues/15930"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3efaead5-6fd7-6bca-3237-820dd1d41b14 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EC46:3618EF:52BBEC:6E727F:6A629BB5 |
| html-safe-nonce | e4fb4665f42557e186c88a436b6b28295e6126ab3b064d6ea5306ed15df6a40b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQzQ2OjM2MThFRjo1MkJCRUM6NkU3MjdGOjZBNjI5QkI1IiwidmlzaXRvcl9pZCI6IjExMTk1MjAwMjY3NjcwMzk0MTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ec6b6fa50f66a075c91f5e0ef86615c237fe7146c2b2f40356c589d58433b220 |
| hovercard-subject-tag | issue:4011556720 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/google-cloud-python/15930/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ba14680700aa92fa3bb19e589ca2f910c09bfd72815bc5c212e9ac5613a713a0/googleapis/google-cloud-python/issues/15930 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ba14680700aa92fa3bb19e589ca2f910c09bfd72815bc5c212e9ac5613a713a0/googleapis/google-cloud-python/issues/15930 |
| og:image:alt | Summary When using a multi-database setup with django-google-spanner as the default database and Cloud SQL (MySQL) as a secondary database, the auto-incremented ID from MySQL is not being populated... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | miyamoto-kei |
| hostname | github.com |
| expected-hostname | github.com |
| None | a6443033349a18134b517627f043d4a59a5110337b879810c7229c2c7e28eda0 |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/google-cloud-python git https://github.com/googleapis/google-cloud-python.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 16316451 |
| octolytics-dimension-repository_nwo | googleapis/google-cloud-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 16316451 |
| octolytics-dimension-repository_network_root_nwo | googleapis/google-cloud-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d6c6cacbe32d3ddfd1796fbd1a39b8fb47b87daf |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width