django-add-default-value
django-add-default-value copied to clipboard
Reverse db-level default
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
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?