[Django] #24291: Migrations fail with unused swappable model

16 views
Skip to first unread message

Django

unread,
Feb 6, 2015, 8:07:40 AM2/6/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
----------------------------+--------------------
Reporter: knbk | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
An unused swappable model has no managers, but when trying to migrate the
app containing that model,
`django.db.migrations.state.ModelState.from_model` is called. That method
expects the model to have at least a `_default_manager`. As this isn't the
case, an `AttributeError` is raised:

{{{
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File
"/home/s120374/projects/dev/django/django/core/management/__init__.py",
line 330, in execute_from_command_line
utility.execute()
File
"/home/s120374/projects/dev/django/django/core/management/__init__.py",
line 322, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/s120374/projects/dev/django/django/core/management/base.py",
line 350, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/s120374/projects/dev/django/django/core/management/base.py",
line 401, in execute
output = self.handle(*args, **options)
File
"/home/s120374/projects/dev/django/django/core/management/commands/migrate.py",
line 173, in handle
ProjectState.from_apps(apps),
File "/home/s120374/projects/dev/django/django/db/migrations/state.py",
line 94, in from_apps
model_state = ModelState.from_model(model)
File "/home/s120374/projects/dev/django/django/db/migrations/state.py",
line 348, in from_model
default_manager_name = model._default_manager.name
AttributeError: type object 'Swappable' has no attribute
'_default_manager'
}}}

The same happens when making a new migration using `makemigrations`, and
when migrating an app that doesn't contain the swappable model.

--
Ticket URL: <https://code.djangoproject.com/ticket/24291>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 6, 2015, 8:28:53 AM2/6/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
----------------------------+--------------------------------------

Reporter: knbk | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by knbk):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Test that currently fails:

{{{
diff --git a/tests/migrations/test_state.py
b/tests/migrations/test_state.py
index a7dbdbe..ca7a21e 100644
--- a/tests/migrations/test_state.py
+++ b/tests/migrations/test_state.py
@@ -2,7 +2,7 @@
from django.db import models
from django.db.migrations.operations import RemoveField
from django.db.migrations.state import ProjectState, ModelState,
InvalidBasesError
-from django.test import TestCase
+from django.test import TestCase, override_settings

from .models import (FoodManager, FoodQuerySet, ModelWithCustomBase,
NoMigrationFoodManager)
@@ -543,6 +543,37 @@ def test_manager_refer_correct_model_version(self):
self.assertIsNot(old_model.food_mgr.model,
new_model.food_mgr.model)
self.assertIsNot(old_model.food_qs.model,
new_model.food_qs.model)

+ @override_settings(TEST_SWAPPABLE_MODEL="migrations.SomeFakeModel")
+ def test_create_swappable(self):
+ """
+ Tests making a ProjectState from an Apps
+ """
+
+ new_apps = Apps(["migrations"])
+
+ class Author(models.Model):
+ name = models.CharField(max_length=255)
+ bio = models.TextField()
+ age = models.IntegerField(blank=True, null=True)
+
+ class Meta:
+ app_label = "migrations"
+ apps = new_apps
+ swappable = "TEST_SWAPPABLE_MODEL"
+
+
+ project_state = ProjectState.from_apps(new_apps)
+ author_state = project_state.models['migrations', 'author']
+
+ self.assertEqual(author_state.app_label, "migrations")
+ self.assertEqual(author_state.name, "Author")
+ self.assertEqual([x for x, y in author_state.fields], ["id",
"name", "bio", "age"])
+ self.assertEqual(author_state.fields[1][1].max_length, 255)
+ self.assertEqual(author_state.fields[2][1].null, False)
+ self.assertEqual(author_state.fields[3][1].null, True)
+ self.assertEqual(author_state.options, {"swappable":
"TEST_SWAPPABLE_MODEL"})
+ self.assertEqual(author_state.bases, (models.Model, ))
+

class ModelStateTests(TestCase):
def test_custom_model_base(self):
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:1>

Django

unread,
Feb 6, 2015, 10:02:48 AM2/6/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
----------------------------+--------------------------------------
Reporter: knbk | Owner: knbk
Type: Bug | Status: assigned
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by knbk):

* owner: nobody => knbk
* status: new => assigned
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:2>

Django

unread,
Feb 6, 2015, 10:34:34 AM2/6/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
----------------------------+------------------------------------

Reporter: knbk | Owner: knbk
Type: Bug | Status: assigned
Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by MarkusH):

* cc: MarkusH (added)
* stage: Unreviewed => Accepted


Comment:

PR: https://github.com/django/django/pull/4071

Can you confirm that this also happens on 1.8? In that case, can you
adjust the version on the ticket, please.

--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:3>

Django

unread,
Feb 6, 2015, 10:54:23 AM2/6/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
----------------------------+-------------------------------------

Reporter: knbk | Owner: knbk
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8alpha1

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+-------------------------------------
Changes (by knbk):

* version: master => 1.8alpha1


Comment:

Yes, the same happens on 1.8.

--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:4>

Django

unread,
Feb 15, 2015, 1:15:12 PM2/15/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
---------------------------------+-------------------------------------

Reporter: knbk | Owner: knbk
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8alpha1
Severity: Release blocker | Resolution:

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by MarkusH):

* severity: Normal => Release blocker


--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:5>

Django

unread,
Feb 16, 2015, 3:28:14 PM2/16/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
-------------------------------------+-------------------------------------

Reporter: knbk | Owner: knbk
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8alpha1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:6>

Django

unread,
Feb 17, 2015, 8:16:37 AM2/17/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
---------------------------------+-------------------------------------
Reporter: knbk | Owner: knbk
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8alpha1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
---------------------------------+-------------------------------------
Changes (by MarkusH):

* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:7>

Django

unread,
Feb 17, 2015, 7:13:18 PM2/17/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
-------------------------------------+-------------------------------------

Reporter: knbk | Owner: knbk
Type: Bug | Status: assigned
Component: Migrations | Version: 1.8alpha1
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: 1 => 0


* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:8>

Django

unread,
Feb 18, 2015, 1:11:45 PM2/18/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: knbk
Type: Bug | Status: closed
Component: Migrations | Version: 1.8alpha1
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Markus Holtermann <info@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"15dc8d1c9d3697170a2c59ecaa7a2b4ba58f5990"]:
{{{
#!CommitTicketReference repository=""
revision="15dc8d1c9d3697170a2c59ecaa7a2b4ba58f5990"
Fixed #24291 - Fixed migration ModelState generation with unused swappable
models

Swapped out models don't have a _default_manager unless they have
explicitly defined managers. ModelState.from_model() now accounts for
this case and uses an empty list for managers if no explicit managers
are defined and a model is swapped out.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:9>

Django

unread,
Feb 18, 2015, 1:19:47 PM2/18/15
to django-...@googlegroups.com
#24291: Migrations fail with unused swappable model
-------------------------------------+-------------------------------------
Reporter: knbk | Owner: knbk
Type: Bug | Status: closed
Component: Migrations | Version: 1.8alpha1
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Markus Holtermann <info@…>):

In [changeset:"84c9b24c5afba524c8dcb4bb5f7c40c43291d132"]:
{{{
#!CommitTicketReference repository=""
revision="84c9b24c5afba524c8dcb4bb5f7c40c43291d132"
[1.8.x] Fixed #24291 - Fixed migration ModelState generation with unused
swappable models

Swapped out models don't have a _default_manager unless they have
explicitly defined managers. ModelState.from_model() now accounts for
this case and uses an empty list for managers if no explicit managers
are defined and a model is swapped out.

Backport of 15dc8d1c9d3697170a2c59ecaa7a2b4ba58f5990 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24291#comment:10>

Reply all
Reply to author
Forward
0 new messages