1

I'm tasked with upgrading the Django version for a project that currently uses Django 2.2.24. It contains a model (with existing migrations) that looks roughly like this:

class Membership(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    type = models.ForeignKey(MembershipType, on_delete=None)

Starting with Django 3.0, on_delete=None causes an error since on_delete is supposed to be a callable. In order to avoid the error, both the model and the existing migrations have to be changed.
By itself, it's not an issue to change the model like this:

class Membership(models.Model):
     person = models.ForeignKey(Person, on_delete=models.CASCADE)
     type = models.ForeignKey(MembershipType, on_delete=models.SET_NULL, null=True)

But existing databases are not yet aware that the corresponding field can be nullable, so a new migration is required for that.
The best way I currently see to do this is the following:

  • change the model
  • create&apply a migration using Django 2.2.24
  • change the old migrations manually

Is there a more elegant way to solve this issue?

GreenSmurf
  • 210
  • 1
  • 9

1 Answers1

0

I'm not sure this is the optimal solution, but maybe it will help you find a similar solution at least.

If you can reset the database, then you can find the migration file where the field was first created and change on_delete to SET_NULL and set null=True. Then remove the database and run migrations from scratch.

If you can't remove the database, then you could:

  1. Change the model as your code.
  2. Edit migration file where the field was created. (Same as above).
  3. Manually in the database run the SQL to alter the field to make it nullable.
Felix Eklöf
  • 3,253
  • 2
  • 10
  • 27