Django无法创建django_migrations表问题求助
MigrationSchemaMissing Error (Can't Create django_migrations Table) Hey there, I’ve dealt with this exact headache before—let’s walk through the most likely fixes to get your migrations working again. First, let’s recap the error you’re hitting:
Traceback (most recent call last):
File "manage.py", line 15, in
execute_from_command_line(sys.argv)
File "d:\django\django\core\management_init_.py", line 381, in execute_from_command_line
utility.execute()
File "d:\django\django\core\management_init_.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "d:\django...
报错信息:raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
This error boils down to Django not being able to set up its core migration tracking table, almost always due to database configuration, permissions, or connectivity issues. Here’s what to check step by step:
1. Verify Your Database Configuration in settings.py
Double-check that your DATABASES block has all the correct details—even a tiny typo here can break everything:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Or 'postgres', 'sqlite3', etc. 'NAME': 'your_database_name', 'USER': 'your_db_username', 'PASSWORD': 'your_db_password', 'HOST': 'localhost', # Or your remote DB host 'PORT': '3306', # Match your DB's port } }
- For SQLite: Ensure the file path is correct and your user has read/write access to that directory.
2. Check Database User Permissions
If you’re using MySQL, PostgreSQL, or another client-server DB, your configured user might not have the right permissions to create tables. For example, in MySQL, run these commands (replace placeholders with your details):
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_db_username'@'localhost'; FLUSH PRIVILEGES;
This gives the user full control over the target database, which is necessary for Django to set up migration tables and other schema changes.
3. Manually Create the Database (If Needed)
Django can sometimes fail to auto-create the database itself. Head to your database management tool (phpMyAdmin, pgAdmin, MySQL CLI, etc.) and manually create a database with the exact name specified in settings.py. Then try running migrations again.
4. Test Database Connectivity
Make sure your database service is running, and you can connect to it outside of Django. Use the DB’s native client (like mysql -u username -p for MySQL) to log in—if you can’t connect here, fix that first (check firewall rules, service status, or credentials).
5. Clean Up Stale Migration Files/Cache
Old migration leftovers can cause weird issues. Try these steps:
- Delete all
__pycache__folders in your project and app directories. - In each app’s
migrationsfolder, delete every file except__init__.py. - Run
python manage.py makemigrationsto regenerate fresh migration files, thenpython manage.py migrateto apply them.
6. Confirm Virtual Environment & Dependencies
If you’re using a virtual environment, make sure it’s activated—sometimes you might be running a different Django version or missing DB drivers (like mysqlclient for MySQL, psycopg2-binary for PostgreSQL). Install any missing packages with pip install <package-name>.
Give these steps a shot—most of the time, it’s one of these simple fixes that resolves the issue.
内容的提问来源于stack exchange,提问作者Cyrus

