﻿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
37389	AlterField on parent PK can generate SQL for unmanaged child model	Felix Engelmann	Felix Engelmann	"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."	Bug	assigned	Migrations	5.2	Normal		unmanaged, inheritance, MTI	Felix Engelmann	Accepted	1	0	0	1	0	0
