diff options
| author | Thibaut Decombe <thibaut.decombe@gmail.com> | 2025-04-26 17:02:57 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-05-12 17:39:20 +0100 |
| commit | 825ddda26a14847c30522f4d1112fb506123420d (patch) | |
| tree | 2ce7b24601486e26a48e191bd58082910083391e /tests/migrations | |
| parent | 8be0c0d6901669661fca578f474cd51cd284d35a (diff) | |
Fixed #33174 -- Fixed migrations crash for model inheriting from Generic[T].
Diffstat (limited to 'tests/migrations')
6 files changed, 115 insertions, 0 deletions
diff --git a/tests/migrations/migrations_test_apps/with_generic_model/__init__.py b/tests/migrations/migrations_test_apps/with_generic_model/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/migrations/migrations_test_apps/with_generic_model/__init__.py diff --git a/tests/migrations/migrations_test_apps/with_generic_model/migrations/0001_initial.py b/tests/migrations/migrations_test_apps/with_generic_model/migrations/0001_initial.py new file mode 100644 index 0000000000..efb998918a --- /dev/null +++ b/tests/migrations/migrations_test_apps/with_generic_model/migrations/0001_initial.py @@ -0,0 +1,40 @@ +import typing + +from django.db import migrations, models + + +class Migration(migrations.Migration): + initial = True + + operations = [ + migrations.CreateModel( + name="GenericModel", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ], + bases=(typing.Generic, models.Model), + ), + migrations.CreateModel( + name="GenericModelPEP695", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ], + bases=(models.Model, typing.Generic), + ), + ] diff --git a/tests/migrations/migrations_test_apps/with_generic_model/migrations/0002_customgenericmodel.py b/tests/migrations/migrations_test_apps/with_generic_model/migrations/0002_customgenericmodel.py new file mode 100644 index 0000000000..7d40eec5bf --- /dev/null +++ b/tests/migrations/migrations_test_apps/with_generic_model/migrations/0002_customgenericmodel.py @@ -0,0 +1,31 @@ +from django.db import migrations, models + +from ..models import Child + + +class Migration(migrations.Migration): + + dependencies = [ + ("with_generic_model", "0001_initial"), + ] + + operations = [ + migrations.CreateModel( + name="CustomGenericModel", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ], + bases=( + Child, + models.Model, + ), + ), + ] diff --git a/tests/migrations/migrations_test_apps/with_generic_model/migrations/__init__.py b/tests/migrations/migrations_test_apps/with_generic_model/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/migrations/migrations_test_apps/with_generic_model/migrations/__init__.py diff --git a/tests/migrations/migrations_test_apps/with_generic_model/models.py b/tests/migrations/migrations_test_apps/with_generic_model/models.py new file mode 100644 index 0000000000..2cedbe2f5b --- /dev/null +++ b/tests/migrations/migrations_test_apps/with_generic_model/models.py @@ -0,0 +1,36 @@ +import typing + +from django.db import models + +T = typing.TypeVar("T") + + +class GenericModel(typing.Generic[T], models.Model): + """A model inheriting from typing.Generic.""" + + +class GenericModelPEP695[T](models.Model): + """A model inheriting from typing.Generic via the PEP 695 syntax.""" + + +# Example from Python docs: +# https://typing.python.org/en/latest/spec/generics.html#arbitrary-generic-types-as-base-classes +T1 = typing.TypeVar("T1") +T2 = typing.TypeVar("T2") +T3 = typing.TypeVar("T3") + + +class Parent1(typing.Generic[T1, T2]): + pass + + +class Parent2(typing.Generic[T1, T2]): + pass + + +class Child(Parent1[T1, T3], Parent2[T2, T3]): + pass + + +class CustomGenericModel(Child[T1, T3, T2], models.Model): + """A model inheriting from a custom subclass of typing.Generic.""" diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 00f97c5f3a..f1373eca1e 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -1551,6 +1551,14 @@ class MigrateTests(MigrationTestBase): recorder.record_unapplied("migrations2", "0002_second") recorder.record_unapplied("migrations2", "0001_squashed_0002") + @override_settings( + INSTALLED_APPS=[ + "migrations.migrations_test_apps.with_generic_model", + ] + ) + def test_migrate_model_inherit_generic(self): + call_command("migrate", verbosity=0) + class MakeMigrationsTests(MigrationTestBase): """ |
