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

Viewed 49

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?

2 Answers

in foreignkey attribute add default=None

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