Skip to content

Commit 71d805f

Browse files
author
Ilya Gurov
authored
feat: support foreign keys (#802)
1 parent d7107bd commit 71d805f

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/django-google-spanner/django_spanner/features.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
2323
has_case_insensitive_like = False
2424
# https://cloud.google.com/spanner/quotas#query_limits
2525
max_query_params = 900
26-
supports_foreign_keys = False
26+
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
27+
supports_foreign_keys = False
28+
else:
29+
supports_foreign_keys = True
2730
can_create_inline_fk = False
2831
supports_ignore_conflicts = False
2932
supports_partial_indexes = False
@@ -1343,7 +1346,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
13431346
"many_to_one.tests.ManyToOneTests.test_add_after_prefetch", # noqa
13441347
"many_to_one.tests.ManyToOneTests.test_add_then_remove_after_prefetch", # noqa
13451348
"many_to_one.tests.ManyToOneTests.test_cached_foreign_key_with_to_field_not_cleared_by_save", # noqa
1346-
"many_to_one.tests.ManyToOneTests.test_multiple_foreignkeys", # noqa
13471349
"many_to_one.tests.ManyToOneTests.test_reverse_foreign_key_instance_to_field_caching", # noqa
13481350
"many_to_one.tests.ManyToOneTests.test_set_after_prefetch", # noqa
13491351
"many_to_one_null.tests.ManyToOneNullTests.test_add_efficiency", # noqa
@@ -1509,7 +1511,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
15091511
"ordering.tests.OrderingTests.test_stop_slicing", # noqa
15101512
"ordering.tests.OrderingTests.test_stop_start_slicing", # noqa
15111513
"queries.test_bulk_update.BulkUpdateNoteTests.test_batch_size", # noqa
1512-
"queries.test_bulk_update.BulkUpdateNoteTests.test_foreign_keys_do_not_lookup", # noqa
15131514
"queries.test_bulk_update.BulkUpdateNoteTests.test_functions", # noqa
15141515
"queries.test_bulk_update.BulkUpdateNoteTests.test_set_field_to_null", # noqa
15151516
"queries.test_bulk_update.BulkUpdateNoteTests.test_set_mixed_fields_to_null", # noqa

packages/django-google-spanner/django_spanner/schema.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
# Use of this source code is governed by a BSD-style
44
# license that can be found in the LICENSE file or at
55
# https://developers.google.com/open-source/licenses/bsd
6-
6+
import os
77
import uuid
8+
89
from django.db import NotSupportedError
910
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
1011
from django_spanner._opentelemetry_tracing import trace_call
@@ -21,7 +22,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
2122
"CREATE TABLE %(table)s (%(definition)s) PRIMARY KEY(%(primary_key)s)"
2223
)
2324
sql_delete_table = "DROP TABLE %(table)s"
24-
sql_create_fk = None
25+
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
26+
sql_create_fk = None
27+
else:
28+
sql_create_fk = (
29+
"ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) "
30+
"REFERENCES %(to_table)s (%(to_column)s)"
31+
)
2532
# Spanner doesn't support partial indexes. This string omits the
2633
# %(condition)s placeholder so that partial indexes are ignored.
2734
sql_create_index = (

0 commit comments

Comments
 (0)