From 5d5a4ce44b2f244e1f7a81ce02ce14bb3f43660b Mon Sep 17 00:00:00 2001 From: Ben Cail Date: Thu, 15 Feb 2024 14:13:09 -0500 Subject: [PATCH] update tablespace instances to check OPTIONS first --- django/db/backends/base/schema.py | 30 ++++++++++++++++++++++++----- django/db/models/fields/__init__.py | 8 +++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 3e38c56d50..3e64d6d81b 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -290,10 +290,19 @@ class BaseDatabaseSchemaEditor: if statement ), } - if model._meta.db_tablespace: - tablespace_sql = self.connection.ops.tablespace_sql( - model._meta.db_tablespace - ) + db_tablespace = None + if ( + settings.DATABASES[self.connection.alias] + .get("OPTIONS", {}) + .get("DEFAULT_TABLESPACE") + ): + db_tablespace = settings.DATABASES[self.connection.alias]["OPTIONS"][ + "DEFAULT_TABLESPACE" + ] + elif model._meta.db_tablespace: + db_tablespace = model._meta.db_tablespace + if db_tablespace: + tablespace_sql = self.connection.ops.tablespace_sql(db_tablespace) if tablespace_sql: sql += " " + tablespace_sql return sql, params @@ -359,7 +368,13 @@ class BaseDatabaseSchemaEditor: elif field.unique: yield "UNIQUE" # Optionally add the tablespace if it's an implicitly indexed column. - tablespace = field.db_tablespace or model._meta.db_tablespace + tablespace = ( + field.db_tablespace + or settings.DATABASES[self.connection.alias] + .get("OPTIONS", {}) + .get("DEFAULT_TABLESPACE") + or model._meta.db_tablespace + ) if ( tablespace and self.connection.features.supports_tablespaces @@ -1517,10 +1532,15 @@ class BaseDatabaseSchemaEditor: def _get_index_tablespace_sql(self, model, fields, db_tablespace=None): if db_tablespace is None: + db_options = settings.DATABASES[self.connection.alias].get("OPTIONS", {}) if len(fields) == 1 and fields[0].db_tablespace: db_tablespace = fields[0].db_tablespace + elif db_options.get("DEFAULT_INDEX_TABLESPACE"): + db_tablespace = db_options["DEFAULT_INDEX_TABLESPACE"] elif settings.DEFAULT_INDEX_TABLESPACE: db_tablespace = settings.DEFAULT_INDEX_TABLESPACE + elif db_options.get("DEFAULT_TABLESPACE"): + db_tablespace = db_options["DEFAULT_TABLESPACE"] elif model._meta.db_tablespace: db_tablespace = model._meta.db_tablespace if db_tablespace is not None: diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index d1f31f0211..881afc8253 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -922,7 +922,13 @@ class Field(RegisterLookupMixin): @property def db_tablespace(self): - return self._db_tablespace or settings.DEFAULT_INDEX_TABLESPACE + return ( + self._db_tablespace + or settings.DATABASES[connection.alias] + .get("OPTIONS", {}) + .get("DEFAULT_INDEX_TABLESPACE") + or settings.DEFAULT_INDEX_TABLESPACE + ) @property def db_returning(self):