Django静态预编译报错:relation 'static_precompiler_dependency'不存在
You're hitting an error where Django throws:
Exception Value: relation "static_precompiler_dependency" does not exist
even though you can see the COMPILED folder generated. Let's break down why this happens and how to fix it:
Root Cause
The django-static-precompiler library uses the static_precompiler_dependency database table to track dependencies between your SCSS files (like imports). Even if it can compile your files initially, it needs this table to handle subsequent requests and dependency checks—missing it causes the runtime error.
Step-by-Step Fixes
1. Run the Required Database Migrations
The library includes built-in migration files to create this table. Run this command in your project root:
python manage.py migrate static_precompiler
This will create the static_precompiler_dependency table in your database.
2. Confirm static_precompiler is in Your Installed Apps
Make sure you've added the app to your INSTALLED_APPS in settings.py—this is necessary for Django to load its migrations and functionality:
INSTALLED_APPS = [ # Core Django apps (e.g., 'django.contrib.admin', 'django.contrib.auth') # Your custom apps 'static_precompiler', # Add this line if missing ]
3. Clean Up Your STATIC_ROOT Configuration (Optional but Recommended)
Your current STATIC_ROOT points to static/css, which overlaps with your STATICFILES_DIRS (already pointing to the project's static folder). STATIC_ROOT is meant to be a separate directory where Django collects all static files for production deployment. Update it to avoid conflicts:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Verify the Fix
After completing these steps, restart your Django development server and refresh your page. The error should be gone, and your compiled CSS should load correctly.
内容的提问来源于stack exchange,提问作者Wariored

