Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Migrate prompts if you need makemigrations, runserver prompts for mig…

  • Loading branch information...
commit 1d20e6df9553346c79edd92e6e8934e9c5c4aa2c 1 parent abb04f1
@andrewgodwin andrewgodwin authored
12 django/core/management/commands/
@@ -14,6 +14,9 @@
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.loader import MigrationLoader, AmbiguityError
+from django.db.migrations.state import ProjectState
+from django.db.migrations.autodetector import MigrationAutodetector
+from django.db.models.loading import cache
from django.utils.module_loading import module_has_submodule
@@ -120,6 +123,15 @@ def handle(self, *args, **options):
if not plan:
if self.verbosity >= 1:
self.stdout.write(" No migrations needed.")
+ # If there's changes that aren't in migrations yet, tell them how to fix it.
+ autodetector = MigrationAutodetector(
+ executor.loader.graph.project_state(),
+ ProjectState.from_app_cache(cache),
+ )
+ changes = autodetector.changes(graph=executor.loader.graph)
+ if changes:
+ self.stdout.write(" Your models have changes that are not yet reflected in a migration, and so won't be applied."))
+ self.stdout.write(" Run ' makemigrations' to make new migrations, and then re-run ' migrate' to apply them."))
executor.migrate(targets, plan, fake=options.get("fake", False))
14 django/core/management/commands/
@@ -10,6 +10,8 @@
from import BaseCommand, CommandError
from django.core.servers.basehttp import run, get_internal_wsgi_application
+from django.db import connections, DEFAULT_DB_ALIAS
+from django.db.migrations.executor import MigrationExecutor
from django.utils import autoreload
from django.utils import six
@@ -99,10 +101,10 @@ def inner_run(self, *args, **options):
self.stdout.write("Validating models...\n\n")
+ self.check_migrations()
now ='%B %d, %Y - %X')
if six.PY2:
now = now.decode('utf-8')
"Django version %(version)s, using settings %(settings)r\n"
@@ -144,6 +146,16 @@ def inner_run(self, *args, **options):
+ def check_migrations(self):
+ """
+ Checks to see if the set of migrations on disk matches the
+ migrations in the database. Prints a warning if they don't match.
+ """
+ executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
+ plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
+ if plan:
+ self.stdout.write("\nYou have unapplied migrations; your app may not work properly until they are applied."))
+ self.stdout.write("Run 'python migrate' to apply them.\n"))
# Kept for backward compatibility
BaseRunserverCommand = Command
Please sign in to comment.
Something went wrong with that request. Please try again.