Skip to content

chore(django-google-spanner): enable check constraints on Spanner Emulator - #19094

Merged
olavloite merged 3 commits into
mainfrom
spanner-django-enable-check-constraints-on-emulator
Nov 19, 2026
Merged

olavloite merged 3 commits into
mainfrom
spanner-django-enable-check-constraints-on-emulator

Conversation

@olavloite

Copy link
Copy Markdown
Contributor

The Cloud Spanner Django backend previously disabled column and table check constraints when running against the Spanner Emulator, which caused inconsistent DDL and led alter_field migrations to emit "None;" on the emulator.

The Spanner Emulator now supports creating, altering, introspecting, and enforcing CHECK constraints, so check-constraint support is now enabled unconditionally across both live Spanner and the emulator.

This change also:

  • Filters Spanner's internal synthetic NOT NULL check constraints out of constraint introspection so altering a non-nullable field to remove a check constraint does not attempt to drop an internal constraint.
  • Updates Django backend test skips and test-only identifier sanitization for newly enabled upstream tests that rely on unsupported Spanner features (column renames, JSON equality, and SELECT ... WHERE without a FROM clause).
  • Updates the emulator integration workflow and nox configuration to run the package system tests against the emulator on Python 3.12 and 3.14 (covering Django 5.2 and 6.0), re-enables the check-constraint system test on the emulator, and temporarily skips the failing JSON lookup system test (tracked in fix(django-google-spanner): JSONField key-transform equality lookup fails on Spanner #19088).

@olavloite
olavloite requested review from a team as code owners November 16, 2026 09:38

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

Copy link
Copy Markdown
Contributor

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 enables support for column and table check constraints by default (removing emulator-specific bypasses), filters out synthetic CK_IS_NOT_NULL_* constraints during introspection, and updates identifier quoting to handle the '>' character. It also updates Nox configurations to run system tests against the emulator across multiple Django versions. The review feedback highlights critical issues where CheckConstraint was incorrectly updated to use 'condition' instead of 'check', which will cause runtime TypeErrors. Additionally, it is recommended to safely traverse the exception chain in tests rather than directly chaining 'cause' to prevent potential AttributeErrors.

…lator

The Cloud Spanner Django backend previously disabled column and table check
constraints when running against the Spanner Emulator, which caused
inconsistent DDL and led `alter_field` migrations to emit `"None;"` on
the emulator.

The Spanner Emulator now supports creating, altering, introspecting, and
enforcing `CHECK` constraints, so check-constraint support is now enabled
unconditionally across both live Spanner and the emulator.

This change also:
- Filters Spanner's internal synthetic `NOT NULL` check constraints out of
  constraint introspection so altering a non-nullable field to remove a
  check constraint does not attempt to drop an internal constraint.
- Updates Django backend test skips and test-only identifier sanitization
  for newly enabled upstream tests that rely on unsupported Spanner
  features (column renames, JSON equality, and `SELECT ... WHERE` without
  a `FROM` clause).
- Updates the emulator integration workflow and nox configuration to run
  the package system tests against the emulator on Python 3.12 and 3.14
  (covering Django 5.2 and 6.0), re-enables the check-constraint system
  test on the emulator, and temporarily skips the failing JSON lookup
  system test (tracked in #19088).
@olavloite
olavloite force-pushed the spanner-django-enable-check-constraints-on-emulator branch from 2ee210d to e33f4d3 Compare November 16, 2026 10:21
@olavloite

Copy link
Copy Markdown
Contributor Author

/gemini review

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

Copy link
Copy Markdown
Contributor

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 enables support for column and table check constraints unconditionally, removing emulator-specific exclusions since the Spanner emulator now supports them. It also updates constraint introspection to filter out synthetic CK_IS_NOT_NULL_ constraints, adjusts identifier quoting to handle the > operator during backend tests, and updates the test suite accordingly. The reviewer suggested expanding the identifier sanitization in quote_name to also replace other comparison operators like <, =, and ! with underscores to make it more robust.

if constraint_type == "FOREIGN KEY":
# We don't yet support anything related to FOREIGN KEY.
# See https://github.com/googleapis/python-spanner-django/issues/313.
if constraint_type == "FOREIGN KEY" or (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We support FOREIGN KEY right? It wasn't supported in Emulator before.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the emulator supports foreign keys, but this pull request intentionally limits the scope to only fixing CHECK constraints. I've updated the TODO to better explain what the current status is, and added a tracking bug for foreign keys.

return name
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
name = name.replace(" ", "_").replace("-", "_")
name = name.replace(" ", "_").replace("-", "_").replace(">", "_")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why >

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

" ", "-", and ">" are all tokens that Spanner does not allow in identifiers (e.g. table names or constraint names). Normally, if an application tries to use any of those for a table name or constraint name, then we just send them to Spanner and let Spanner return an error. However, these specific replacements are applied only when running the standard Django tests (the ones defined in Django itself). Previously, we only replaced " " and "-", as those were the only ones that were actually used by those tests. Now that we also run the tests for CHECK constraints, we need to replace ">" as well, as the tests contain a constraint that has the name "Generated model check constraint a > 0"

@olavloite
olavloite enabled auto-merge (squash) November 19, 2026 19:13
@olavloite
olavloite merged commit f8d2332 into main Nov 19, 2026
225 of 236 checks passed
@olavloite
olavloite deleted the spanner-django-enable-check-constraints-on-emulator branch November 19, 2026 19:51
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.

2 participants