Django 2.0.4博客应用报错:relation 'blog_blog' does not exist
Hey there, let's sort out this frustrating error you're facing with your Django blog app! That "relation does not exist" message almost always means your database doesn't have the table corresponding to your Blog model yet. Here's how to fix it step by step:
Step 1: Verify your app is registered in settings
First, double-check that your blog app is added to INSTALLED_APPS in your project's settings.py file. Django won't process migrations for apps that aren't listed here:
INSTALLED_APPS = [ # ... other default apps like 'django.contrib.admin' 'blog', # Make sure this line exists! ]
Step 2: Generate migration files
If you haven't already, tell Django to create migration scripts based on your Blog model. Run this command in your terminal:
python manage.py makemigrations
You should see output confirming a migration was created for the blog app (something like Migrations for 'blog': blog/migrations/0001_initial.py).
Step 3: Apply migrations to the database
Next, execute those migrations to actually create the blog_blog table in your database:
python manage.py migrate
This command will apply all pending migrations, including the one for your Blog model.
If the problem persists: Reset migrations (dev environments only!)
If you're still getting the error, it might be due to a mismatch between your migration files and the database state. Only do this if you're working in a development environment (never touch production data this way!):
- Back up your database first (just in case you need to revert)
- Delete all files in your
blog/migrationsdirectory except__init__.py - Drop your existing database (or use
python manage.py flushif you want to keep the database but clear all data) - Re-run these commands in order:
python manage.py makemigrations python manage.py migrate
Your Blog model looks correctly structured from the code snippet you shared, so these steps should get your table created and resolve the error.
内容的提问来源于stack exchange,提问作者Serhii

