“no such column” after adding a field to the model

python manage.py migrate --fake
python manage.py makemigrations
python manage.py migrate

0
0
JayZ4Lyf 95 points

                                    python manage.py makemigrations
python manage.py migrate
python manage.py migrate --fake

0
0
4.67
3
Javiator 135 points

                                    #Here is the resolve.sh that calls python and migrations (has to be placed in the same folder as your manage.py file, like the .py files) :
..................................................................



python manage.py shell < check.py

read -p "Does it seem ok ? (y for ok/n for remigrate)" migrateok

if [ $migrateok = "n" ]
then
    python manage.py shell < rm_badcolumns.py

    python manage.py makemigrations
    sleep 1
    python manage.py migrate

    python manage.py shell < resume_badcolumns.py

    python manage.py makemigrations
    sleep 1
    python manage.py migrate

    echo It should work now..
else
    echo No changes were made..
fi


..................................................................................
#check.py checks if there are issues with some models in the database :
......................................................................................



from <app>.models import *
import os, sys
from shutil import move
from django.db import connection

tables = connection.introspection.table_names()
seen_models = connection.introspection.installed_models(tables)

errorColumn = []
is_fine = "EVERYTHING LOOKS FINE!"
#looping through models to detect issues
for model in seen_models:
    try:
        print(model.objects.all())
    except:
        is_fine = "WARNING SOME MODELS ARE CORRUPTED"

print(is_fine)


..............................................................................
#This check will tell the user if some models are not sync with your database.

If the user chooses to "re-migrate", it calls rm_badcolumns.py :
...........................................................................



from <app>.models import *
import os, sys
from shutil import move
from django.db import connection


tables = connection.introspection.table_names()
seen_models = connection.introspection.installed_models(tables)

errorColumn = []

for model in seen_models:
    try:
        model.objects.all()
    except:
        errorColumn.append(str(sys.exc_info()[1])[30::]+' =')
        #this weird expression just get the column that causes trouble
        #you may have to adapt indexes depending on error output in exc_info

os.chdir('./<app>/')

#removing columns from models.py based on the error pattern
def replace(pattern, subst):
    with open('models_sub.py','w') as fout:
        with open('models.py','r') as models:
            for line in models:
                fout.write(line.replace(pattern, subst))

    os.remove('models.py')
    move('models_sub.py','models.py')

#applying this func to each error, and commenting out columns with an error mark to catch them back later
for errorStr in errorColumn:
    replace(errorStr, '#error_mark '+errorStr)

print 'Removing troublesome column for re-entering..'
...............................................................................
#Then resolve.sh will start another migration. We uncomment the troublesome columns with resume_badcolumns.py to get them migrated again :
................................................................................


from <app>.models import *
import os, sys
from shutil import move
from django.db import connection


os.chdir('./<app>/')

#same search and replace func but returning True if sthg was replaced, and False otherwise
def replace(pattern, subst):
    a = False
    with open('models_sub.py','w') as fout:
        with open('models.py','r') as models:
            for line in models:
                fout.write(line.replace(pattern, subst))
                if pattern in line:
                    a = True
    os.remove('models.py')
    move('models_sub.py','models.py')

    return a

a = True
#while error_marks are replaced, we go on looping
while a:
    a = replace('#error_mark ', '')

print 'Re-adding troublesome columns..'
.......................................................................

We make a final migration and everything should be fine. 

4.67 (3 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source