Django部署Heroku后PostgreSQL迁移未生效,仍触发DataError
django.db.utils.DataError After Updating Django Model Field Length on Heroku Hey there, let's work through this frustrating issue together. You updated the max_length of your url field to 200, generated a migration, pushed it to Heroku—but the migration isn't applying, and you're still getting that "value too long" error. Here's how to troubleshoot and fix it:
Step 1: Verify the Migration File is Actually on Heroku
Even though you think it's there, let's double-check. Run this command in your local terminal to list the migration files on Heroku:
heroku run ls your_app_name/migrations/
Replace your_app_name with the actual name of your Django app. Look for the migration file you generated (it'll look like 000X_alter_articlescheme_url.py or similar). If it's missing:
- Check your
.gitignorefile to make sure it isn't excluding migration files (you should only ignore__pycache__or*.pyc, not the.pymigration files). - Add the migration file locally:
git add your_app_name/migrations/000X_xxxx.py - Commit and push again:
git commit -m "Add migration for url field max_length update" git push heroku main
Step 2: Check Migration Status and Force Application
Next, confirm if Heroku thinks the migration has been applied. Run:
heroku run python3 manage.py showmigrations your_app_name
Look for your migration entry.
- If it doesn't have an
[X]next to it, run the migrate command again:heroku run python3 manage.py migrate your_app_name - If it does have an
[X]but the database field still hasn't updated (we'll confirm that next), you'll need to roll back and reapply the migration:- Find the migration number before your latest one (e.g., if your latest is
0003, use0002):heroku run python3 manage.py migrate your_app_name 0002 - Then reapply the latest migration:
heroku run python3 manage.py migrate your_app_name
- Find the migration number before your latest one (e.g., if your latest is
Step 3: Confirm the Database Field Was Updated
Let's check the actual database schema to make sure the url field's length changed. Connect to your Heroku PostgreSQL database:
heroku pg:psql
Once connected, run this command to view the table structure (replace appname_articlescheme with your actual table name—Django auto-generates it as appname_modelname):
\d appname_articlescheme
Look for the url row—its type should be character varying(200). If it's still (30), the migration didn't run correctly, and you'll need to redo the migration locally:
- Delete the problematic migration file from your local
migrationsfolder. - Regenerate the migration:
python3 manage.py makemigrations your_app_name - Open the new migration file to verify it includes an
AlterFieldoperation for theurlfield settingmax_length=200. - Push this new migration to Heroku and run
migrateagain.
Step 4: Last Resort—Manual Database Update (Use Carefully)
If all else fails, you can directly modify the database field via SQL (note: this bypasses Django's migration system, so only do this if you're sure it's safe):
- Connect to your Heroku DB as before with
heroku pg:psql. - Run this SQL command (replace the table name with yours):
ALTER TABLE appname_articlescheme ALTER COLUMN url TYPE VARCHAR(200); - Exit the psql shell with
\q, then test your import script again.
After any of these steps, rerun your import script—it should no longer throw the length error.
内容的提问来源于stack exchange,提问作者Rahmi Pruitt

