Skip to content

make SchemaEditor.alter_field() populate default on NULL to NOT NULL change #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"schema.tests.SchemaTests.test_text_field_with_db_index",
# AlterField
"schema.tests.SchemaTests.test_alter_field_add_index_to_integerfield",
"schema.tests.SchemaTests.test_alter_field_default_dropped",
"schema.tests.SchemaTests.test_alter_field_fk_keeps_index",
"schema.tests.SchemaTests.test_alter_field_fk_to_o2o",
"schema.tests.SchemaTests.test_alter_field_o2o_keeps_unique",
"schema.tests.SchemaTests.test_alter_field_o2o_to_fk",
"schema.tests.SchemaTests.test_alter_int_pk_to_int_unique",
"schema.tests.SchemaTests.test_alter_not_unique_field_to_primary_key",
"schema.tests.SchemaTests.test_alter_null_to_not_null",
# AlterField (db_index)
"schema.tests.SchemaTests.test_alter_renames_index",
"schema.tests.SchemaTests.test_indexes",
Expand Down
11 changes: 8 additions & 3 deletions django_mongodb/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ def _alter_field(
new_db_params,
strict=False,
):
collection = self.connection.database[model._meta.db_table]
# Have they renamed the column?
if old_field.column != new_field.column:
self.connection.database[model._meta.db_table].update_many(
{}, {"$rename": {old_field.column: new_field.column}}
)
collection.update_many({}, {"$rename": {old_field.column: new_field.column}})
# Replace NULL with the field default if the field and was changed from
# NULL to NOT NULL.
if new_field.has_default() and old_field.null and not new_field.null:
column = new_field.column
default = self.effective_default(new_field)
collection.update_many({column: {"$eq": None}}, [{"$set": {column: default}}])

def remove_field(self, model, field):
# Remove implicit M2M tables.
Expand Down