3YOURMIND / django-add-default-value

This django Migration Operation can be used to transfer a Fields default value to the database scheme.
Apache License 2.0
138 stars 19 forks source link

Reverse db-level default #37

Open rpradal opened 2 years ago

rpradal commented 2 years ago

Hi,

I am trying to add a new non nullable field on a model in a backward compatible way. I understood clearly what this library achieves, but i am wondering how i can clear the default at the end.

For my first migration i have the following operations :

    operations = [
        migrations.AddField(
            model_name="transaction",
            name="currency",
            field=models.CharField(default="EUR", max_length=3),
        ),
        AddDefaultValue(model_name="transaction", name="currency", value="EUR"),
    ]

And for the second one :

    operations = [
        migrations.AlterField(
            model_name="transaction",
            name="currency",
            field=models.CharField(max_length=3),
        ),
    ]

This second migration does not generate any sql code so i end up with a remaining default at db level.

If there a way to remove the db level default introduced by AddDefaultValue ?

Thanks

Isokaeder commented 2 years ago

Interesting @rpradal . At first glance I would do something like this:

class RemoveDefaultValue(AddDefaultValue):
  def database_forwards(*args, **kwargs):
     super().database_backwards(*args, **kwargs)

  def database_backwards(*args, **kwargs):
     super().database_forwards(*args, **kwargs)

Can you see if that approach works for you?