Opened 61 minutes ago
#37389 new Bug
avoid changing relations from unmanaged models
| Reported by: | Felix Engelmann | Owned by: | |
|---|---|---|---|
| Component: | Migrations | Version: | 6.1 |
| Severity: | Normal | Keywords: | unmanaged, inheritance |
| Cc: | Felix Engelmann | Triage Stage: | Unreviewed |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
The main issue was applying old migrations of models that inherit but are unmanaged. A prime example is a database view that extends a real model, both in terms of python functions and with database columns.
The model.py is as follows:
from django.db import models
class Parent(models.Model):
name = models.CharField()
class Child(Parent):
class Meta:
managed = False
The main culprit happened, when the DEFAULT_AUTO_FIELD setting changed e.g. from django.db.models.AutoField to BigAutoField. This lead makemigrations to create an operation:
migrations.AlterField(
model_name='parent',
name='id',
field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
This migration in the current code tried to follow the parent_ptr OneToOne relation and update the foreign key in the Child table. As Child is a view, this failed on postgres.
The proposed fix is to exclude unmanaged models in the _is_relevant_relation function to make old migrations run.
This migration works well in django 3.