Skip to content

Commit

Permalink
Support GeneratedField (#335)
Browse files Browse the repository at this point in the history
* Support GeneratedField
  • Loading branch information
dauinsight authored Jan 19, 2024
1 parent d362497 commit 6458503
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mssql/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
supports_default_keyword_in_insert = True
supports_expression_defaults = True
supports_default_keyword_in_bulk_insert = True
supports_stored_generated_columns = True
supports_virtual_generated_columns = True

@cached_property
def has_zoneinfo_database(self):
Expand Down
14 changes: 14 additions & 0 deletions mssql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ def _delete_deferred_unique_indexes_for_field(self, field):
def _add_deferred_unique_index_for_field(self, field, statement):
self._deferred_unique_indexes[str(field)].append(statement)

def _column_generated_sql(self, field):
"""Return the SQL to use in a GENERATED ALWAYS clause."""
expression_sql, params = field.generated_sql(self.connection)
persistency_sql = "PERSISTED" if field.db_persist else ""
if params:
expression_sql = expression_sql % tuple(self.quote_value(p) for p in params)
return f"AS {expression_sql} {persistency_sql}"

def _alter_field(self, model, old_field, new_field, old_type, new_type,
old_db_params, new_db_params, strict=False):
"""Actually perform a "physical" (non-ManyToMany) field update."""
Expand Down Expand Up @@ -1016,6 +1024,9 @@ def add_field(self, model, field):
# It might not actually have a column behind it
if definition is None:
return
# Remove column type from definition if field is generated
if (django_version >= (5,0) and field.generated):
definition = definition[definition.find('AS'):]
# Nullable columns with default values require 'WITH VALUES' to set existing rows
if 'DEFAULT' in definition and field.null:
definition = definition.replace('NULL', 'WITH VALUES')
Expand Down Expand Up @@ -1218,6 +1229,9 @@ def create_model(self, model):
definition, extra_params = self.column_sql(model, field)
if definition is None:
continue
# Remove column type from definition if field is generated
if (django_version >= (5,0) and field.generated):
definition = definition[definition.find('AS'):]

if (self.connection.features.supports_nullable_unique_constraints and
not field.many_to_many and field.null and field.unique):
Expand Down

0 comments on commit 6458503

Please sign in to comment.