srgbnd
srgbnd

Reputation: 5634

How to fix migrations for an app with existing schema while not touching other apps?

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?

Upvotes: 0

Views: 270

Answers (2)

srgbnd
srgbnd

Reputation: 5634

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

Upvotes: 0

itzMEonTV
itzMEonTV

Reputation: 20349

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

Upvotes: 1

Related Questions