31👍
You can manually edit your migration and add AlterField
with default value for a field just before RemoveField
. It should be safe even after applying migration. That will make RemoveField
that will happen after to be reversible.
An example. Having field in model summary
named profit
that was defined before removal like that:
profit = models.PositiveIntegerField(verbose_name='profits')
you should add before RemoveField
of it an AlterField
like that:
migrations.AlterField(
model_name='summary',
name='profit',
field=models.PositiveIntegerField(verbose_name='profits', default=0),
preserve_default=False,
),
5👍
If you’re trying to make future migrations reversible, you could try removing the field as three migrations.
- Make the field nullable
- Data migration. Forward, don’t do anything. Backwards, convert nulls to a stub value.
- Remove the field
Each of these three steps should be reversible.
If you have already run the migration and need to reverse it, you could
- manually add the field, allowing nulls
- convert nulls to a stub value
- manually add the not null constraint
- Migrate with
--fake
to the previous migration
- [Django]-Why Django model signals are not working?
- [Django]-Django REST Framework pagination links do not use HTTPS
- [Django]-Race conditions in django
1👍
The easiest way may be to use migrations.RunSQL
You can edit the migration so your operations
list would look like this:
operations = [
sql=[('alter table foo_test drop test)],
reverse_sql=[('alter table foo_test add test varchar)]
]
That would be a hacky solution, but so would be probably any other.
- [Django]-Django 1.7 upgrade error: AppRegistryNotReady: Apps aren't loaded yet
- [Django]-Validators = [MinValueValidator] does not work in Django
- [Django]-Identify the changed fields in django post_save signal
1👍
Just add ‘default’ and ‘preserve_default’ to your AddField or AddModel in older migration, Django will already know that it must recreate the column with the provided default
- [Django]-Django ModelForm has no model class specified
- [Django]-Installing memcached for a django project
- [Django]-Django default_from_email name