Opened 56 minutes ago
#37497 new Bug
migrations.AddField(field=models.ForeignObject(...)) doesn't migrate backwards
| Reported by: | BestCrazyNoob | Owned by: | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.1 |
| Severity: | Normal | Keywords: | |
| Cc: | BestCrazyNoob | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
As per title, migrations that add a ForeignObject to a model crash when migrating backwards.
Example models:
from django.db import models
class A(models.Model):
x = models.BigIntegerField(
unique=True,
)
class B(models.Model):
x = models.BigIntegerField()
ref = models.ForeignObject(
A,
on_delete=models.DO_NOTHING,
from_fields=("x"),
to_fields=("x"),
null=True,
)
To reproduce:
- Create A and B with only the field 'x';
- makemigrations && migrate;
- Add the ForeignObject;
- makemigrations && migrate;
- migrate back to the previous state.
Traceback:
File ".../lib/python3.13/site-packages/django/db/migrations/migration.py", line 193, in unapply
operation.database_backwards(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
self.app_label, schema_editor, from_state, to_state
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File ".../lib/python3.13/site-packages/django/db/migrations/operations/fields.py", line 128, in database_backwards
schema_editor.remove_field(
~~~~~~~~~~~~~~~~~~~~~~~~~~^
from_model, from_model._meta.get_field(self.name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File ".../lib/python3.13/site-packages/django/db/backends/sqlite3/schema.py", line 350, in remove_field
and not (field.remote_field and field.db_constraint)
^^^^^^^^^^^^^^^^^^^
AttributeError: 'ForeignObject' object has no attribute 'db_constraint'
Note:
See TracTickets
for help on using tickets.