Home > Net >  How to add a new foreign key field in model django without migration error?
How to add a new foreign key field in model django without migration error?

Time:06-03

I have two models A, B, they contain a lot of data(int database). Suddenly I decided to add a foreign key in model B to A. But Django can't migrate it because existing rows in table B haven't value for this FK. What should I do in such a situation?

CodePudding user response:

in foreignkey attribute add default=None

CodePudding user response:

from django.db import models
class ModelB(models.Model):
column_one = models.ForeignKey(ModelA, on_delete=models.CASCADE, default = None)
  • Related