﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37497	migrations.AddField(field=models.ForeignObject(...)) doesn't migrate backwards	BestCrazyNoob		"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'
}}}
"	Bug	new	Database layer (models, ORM)	6.1	Normal			BestCrazyNoob	Unreviewed	0	0	0	0	0	0
