django-add-default-value icon indicating copy to clipboard operation
django-add-default-value copied to clipboard

Reverse db-level default

Open rpradal opened this issue 4 years ago • 1 comments

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

rpradal avatar Dec 05 '21 11:12 rpradal

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?

Isokaeder avatar Feb 02 '22 12:02 Isokaeder