Skip to content

Commit

Permalink
feat: support foreign keys (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaFaer authored Nov 10, 2022
1 parent 0c60943 commit 3426ab9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 4 additions & 3 deletions django_spanner/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
has_case_insensitive_like = False
# https://cloud.google.com/spanner/quotas#query_limits
max_query_params = 900
supports_foreign_keys = False
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
supports_foreign_keys = False
else:
supports_foreign_keys = True
can_create_inline_fk = False
supports_ignore_conflicts = False
supports_partial_indexes = False
Expand Down Expand Up @@ -1343,7 +1346,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"many_to_one.tests.ManyToOneTests.test_add_after_prefetch", # noqa
"many_to_one.tests.ManyToOneTests.test_add_then_remove_after_prefetch", # noqa
"many_to_one.tests.ManyToOneTests.test_cached_foreign_key_with_to_field_not_cleared_by_save", # noqa
"many_to_one.tests.ManyToOneTests.test_multiple_foreignkeys", # noqa
"many_to_one.tests.ManyToOneTests.test_reverse_foreign_key_instance_to_field_caching", # noqa
"many_to_one.tests.ManyToOneTests.test_set_after_prefetch", # noqa
"many_to_one_null.tests.ManyToOneNullTests.test_add_efficiency", # noqa
Expand Down Expand Up @@ -1509,7 +1511,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"ordering.tests.OrderingTests.test_stop_slicing", # noqa
"ordering.tests.OrderingTests.test_stop_start_slicing", # noqa
"queries.test_bulk_update.BulkUpdateNoteTests.test_batch_size", # noqa
"queries.test_bulk_update.BulkUpdateNoteTests.test_foreign_keys_do_not_lookup", # noqa
"queries.test_bulk_update.BulkUpdateNoteTests.test_functions", # noqa
"queries.test_bulk_update.BulkUpdateNoteTests.test_set_field_to_null", # noqa
"queries.test_bulk_update.BulkUpdateNoteTests.test_set_mixed_fields_to_null", # noqa
Expand Down
11 changes: 9 additions & 2 deletions django_spanner/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd

import os
import uuid

from django.db import NotSupportedError
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django_spanner._opentelemetry_tracing import trace_call
Expand All @@ -21,7 +22,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
"CREATE TABLE %(table)s (%(definition)s) PRIMARY KEY(%(primary_key)s)"
)
sql_delete_table = "DROP TABLE %(table)s"
sql_create_fk = None
if os.environ.get("RUNNING_SPANNER_BACKEND_TESTS") == "1":
sql_create_fk = None
else:
sql_create_fk = (
"ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) "
"REFERENCES %(to_table)s (%(to_column)s)"
)
# Spanner doesn't support partial indexes. This string omits the
# %(condition)s placeholder so that partial indexes are ignored.
sql_create_index = (
Expand Down

0 comments on commit 3426ab9

Please sign in to comment.