You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生产服务器上Django迁移报错:relation不存在问题求助

Fixing Django's "relation does not exist" Error During makemigrations

Hey there, sorry to hear you're hitting this roadblock while migrating your local Django setup to production! That ProgrammingError about the structure_structurefields table missing usually boils down to a mismatch between Django's expected database state and what's actually in your production DB. Let's go through actionable steps to fix this:

1. Sync your migration history first

First, make sure your local and production environments have the same set of migration files and applied migrations. Run this command in both environments:

python manage.py showmigrations

Check if any migrations marked as "applied" locally are missing on production. If so, you probably forgot to push the latest migration files to your server—get those uploaded first.

2. Run migrate before makemigrations

It's easy to mix up the order! If you jumped straight to makemigrations without applying existing migrations first, Django won't find the tables it needs. Fix this by running:

python manage.py migrate

This creates all tables defined in unapplied migrations, including structure_structurefields. Once that finishes, try running makemigrations again.

3. Reset migrations (for fresh or messed-up databases)

If your production database is brand new, or your migration history got tangled, you can reset things (warning: this will erase existing data—only do this if you can afford to lose it):

  1. Back up your production database first (critical step!)
  2. Delete all files in your app's migrations folder except __init__.py
  3. Run these commands in order:
    python manage.py makemigrations
    python manage.py migrate --fake-initial
    

The --fake-initial flag tells Django to treat the initial table creation as already done, avoiding duplicate table errors.

4. Target the specific app

If the issue is isolated to the structure app, try generating and applying migrations just for it:

python manage.py makemigrations structure
python manage.py migrate structure

Replace structure with your actual app name if it's different. This ensures Django focuses on the app that owns the missing table.

5. Verify the database directly

If none of the above works, log into your production database and check if the table exists. For example, in PostgreSQL you can run:

SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

If the table truly isn't there, double-check your model's migration files to make sure they include the logic to create structure_structurefields. You might have accidentally deleted or modified that migration file locally before pushing.


内容的提问来源于stack exchange,提问作者user4583167

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:04:58