Skip to content

fix ManyToManyField repointing and add support for column rename #126

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 4 commits into from
Sep 13, 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
22 changes: 4 additions & 18 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
uses_savepoints = False

_django_test_expected_failures = {
# Database defaults not supported: bson.errors.InvalidDocument:
# cannot encode object: <django.db.models.expressions.DatabaseDefault
"basic.tests.ModelInstanceCreationTests.test_save_primary_with_db_default",
# 'NulledTransform' object has no attribute 'as_mql'.
"lookup.tests.LookupTests.test_exact_none_transform",
# "Save with update_fields did not affect any rows."
Expand Down Expand Up @@ -93,7 +90,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"schema.tests.SchemaTests.test_order_index",
"schema.tests.SchemaTests.test_text_field_with_db_index",
# AlterField
"schema.tests.SchemaTests.test_alter",
"schema.tests.SchemaTests.test_alter_auto_field_to_integer_field",
"schema.tests.SchemaTests.test_alter_field_add_index_to_integerfield",
"schema.tests.SchemaTests.test_alter_field_default_dropped",
Expand All @@ -104,13 +100,8 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"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",
"schema.tests.SchemaTests.test_alter_null_to_not_null_keeping_default",
"schema.tests.SchemaTests.test_alter_primary_key_the_same_name",
"schema.tests.SchemaTests.test_autofield_to_o2o",
# AlterField (rename)
"schema.tests.SchemaTests.test_rename",
"schema.tests.SchemaTests.test_rename_keep_db_default",
"schema.tests.SchemaTests.test_rename_keep_null_status",
# AlterField (db_index)
"schema.tests.SchemaTests.test_indexes",
"schema.tests.SchemaTests.test_remove_constraints_capital_letters",
Expand All @@ -125,15 +116,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
# alter_unique_together
"schema.tests.SchemaTests.test_remove_unique_together_does_not_remove_meta_constraints",
"schema.tests.SchemaTests.test_unique_together",
# ManyToManyField
"schema.tests.SchemaTests.test_m2m_rename_field_in_target_model",
"schema.tests.SchemaTests.test_m2m_repoint",
"schema.tests.SchemaTests.test_m2m_repoint_custom",
"schema.tests.SchemaTests.test_m2m_repoint_inherited",
"schema.tests.SchemaTests.test_m2m_through_alter",
"schema.tests.SchemaTests.test_m2m_through_alter_custom",
"schema.tests.SchemaTests.test_m2m_through_alter_inherited",
"schema.tests.SchemaTests.test_m2m_through_remove",
# add/remove_constraint
"schema.tests.SchemaTests.test_composed_constraint_with_fk",
"schema.tests.SchemaTests.test_remove_ignored_unique_constraint_not_create_fk_index",
Expand All @@ -159,7 +141,11 @@ def django_test_expected_failures(self):

django_test_skips = {
"Database defaults aren't supported by MongoDB.": {
# bson.errors.InvalidDocument: cannot encode object:
# <django.db.models.expressions.DatabaseDefault
"basic.tests.ModelInstanceCreationTests.test_save_primary_with_db_default",
"schema.tests.SchemaTests.test_db_default_output_field_resolving",
"schema.tests.SchemaTests.test_rename_keep_db_default",
},
"Insert expressions aren't supported.": {
"bulk_create.tests.BulkCreateTests.test_bulk_insert_now",
Expand Down
6 changes: 3 additions & 3 deletions django_mongodb/fields/auto.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from bson import ObjectId, errors
from django.core import exceptions
from django.db.models.fields import AutoField, Field
from django.db.models.fields import AutoField
from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -31,8 +31,8 @@ def get_prep_value(self, value):
return int(value)
raise ValueError(f"Field '{self.name}' expected an ObjectId but got {value!r}.") from e

def rel_db_type(self, connection):
return Field().db_type(connection=connection)
def db_type(self, connection):
return "ObjectId"

def to_python(self, value):
if value is None or isinstance(value, int):
Expand Down
18 changes: 16 additions & 2 deletions django_mongodb/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,22 @@ def add_field(self, model, field):
{}, [{"$set": {column: self.effective_default(field)}}]
)

def alter_field(self, model, old_field, new_field, strict=False):
pass
def _alter_field(
self,
model,
old_field,
new_field,
old_type,
new_type,
old_db_params,
new_db_params,
strict=False,
):
# 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}}
)

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