Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations do not ignore unmanaged models (unlike syncdb) #11196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions django/db/migrations/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,9 @@ def from_apps(cls, apps):
"Takes in an Apps and returns a ProjectState matching it"
app_models = {}
for model in apps.get_models(include_swapped=True):
model_state = ModelState.from_model(model)
app_models[(model_state.app_label, model_state.name_lower)] = model_state
if model._meta.managed:
model_state = ModelState.from_model(model)
app_models[(model_state.app_label, model_state.name_lower)] = model_state
return cls(app_models)

def __eq__(self, other):
Expand Down
12 changes: 12 additions & 0 deletions tests/migrations/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ class Meta:
app_label = "migrations"
apps = new_apps

class Unmanaged(models.Model):
title = models.CharField(max_length=1000)

class Meta:
app_label = "migrations"
apps = new_apps
managed = False

project_state = ProjectState.from_apps(new_apps)
author_state = project_state.models['migrations', 'author']
author_proxy_state = project_state.models['migrations', 'authorproxy']
Expand All @@ -120,6 +128,10 @@ class Meta:
book_index = models.Index(fields=['title'])
book_index.set_name_with_model(Book)

# unmanaged models should not appear in migrations
with self.assertRaises(KeyError):
project_state.models['migrations', 'unmanaged']

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"])
Expand Down