1

I am currently working on a Django project with around 30 models and there are lots of relations(For example, foreign key relations) between the models.

My doubt is "After 6 months, let's say I want to add a new field(s) in one of the model/table(s) in models.py, and make migrations, the new migration files will get created without affecting the initial migration files which were created 6 months ago."

Will the relations be maintained after adding new columns in different tables? (or) do I have to go to pgadmin console and tweak them accordingly?

One way is to erase all the migrations and start fresh, but this is not recommended often especially if there is production data (or) there are frequent changes in the database schema.

Mahesh
  • 1,117
  • 2
  • 23
  • 42

2 Answers2

1

@Mahesh, You may use --fake-initial to avoid the existing tables error at the time of new migrations(When you want to add new column).

Relation will be maintained since you already declared it and unless you change it in a new migration.

How to add a new field to a model with new Django migrations?

And in Docs: https://docs.djangoproject.com/en/3.0/ref/django-admin/#cmdoption-migrate-fake-initial

Iqbal Hussain
  • 1,077
  • 1
  • 4
  • 12
  • you mentioned "Relation will be maintained since you already declared it"..........declared what and where can you please elaborate? – Mahesh Jan 02 '20 at 07:20
  • **@Mahesh**, Thanks for your query. I meant that, you already addressed relationship between the models. and you want to add a new column. Since you have lots of models as you mentioned 30. So for new migration which will be occurred due to add a new column to a particular model definitely will not change the other relationship which already exist. – Iqbal Hussain Jan 02 '20 at 07:50
1

If you don't change Django version, adding new fields on models will not create any problem, even after many years. But, there are some situations this might create problems. For example, if Django is updated and you have installed the latest version.

Hasan
  • 878
  • 1
  • 8
  • 19