0

Django 1.9.7, db.sqlite3 as DB

I have a Django project with several apps. For app "A", I had migrations, but I deleted them by accident and pushed to the remote git. Also, a lot of new stuff for other apps was pushed to the git during the day. Other apps don't depend on the "A" app models.

Everything worked until I decided to add a new field to the model of the "A" app. I got OperationalError: no such column: error. I tried to make initial migrations for the app "A" python manage.py migrate --fake-initial. I got new migrations but I still have the OperationalError: no such column:.

How to fix "A" app migrations without affecting other apps migrations?

srgbnd
  • 5,404
  • 9
  • 44
  • 80
  • Can you not just revert to a previous commit? – Sayse Nov 22 '16 at 09:54
  • @Sayse Other guys that develop other apps inside this project added already some new stuff and did some merges. Is it possible to fix the migrations without git revert? – srgbnd Nov 22 '16 at 10:10

2 Answers2

1

From git point of view, you can do revert to previous commit.

git revert sha #commit sha of the last commit

OR

git reset --hard HEAD~n #n how many commits to remove.
git push --force

Fixing through django(possible if you didn't add any migrations later.),

python manage.py makemigrations APP_A --empty
python manage.py makemigrations APP_A 
python manage.py migrate --fake
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
0

Unfortunately git revert didn't help me. In the end, I solved the problem by executing the following steps:

1.Manually delete all tables related to the "A" app in db.sqlite3.

2.Create new migrations and db.sqlite3 tables from existing schema:

python manage.py makemigrations A --empty
python manage.py makemigrations A
python manage.py migrate

3.Dump the tables data back into db.sqlite3 from a backup:

sqlite3 ~/Backup/A/db.sqlite3 ".dump table_name" | grep -v "CREATE" | sqlite3 db.sqlite3
srgbnd
  • 5,404
  • 9
  • 44
  • 80