7👍
✅
Django is re-creating the migrations because as you are doing the operation manually inside a RunPython operation, it can not understand the field is added. What you can try is (haven’t tried this myself), subclass AddField operations to create a custom AddField operation, where you can handle the exception. Something like the following could work:
from django.db import migrations, models, ProgrammingError
class AddFieldIfNotExists(migrations.AddField):
def database_forwards(self, app_label, schema_editor, from_state, to_state):
try:
super().database_forwards(app_label, schema_editor, from_state,
to_state)
except ProgrammingError as e: # sometimes it can exist
if "already exists" not in str(e):
raise
class Migration(migrations.Migration):
atomic = False
dependencies = [
('app', '0070_auto_20191023_1203'),
]
operations = [
AddFieldIfNotExists(
model_name='agenda',
name='theme',
field=models.PositiveIntegerField(default=1),
),
]
2👍
You can take advantage of the --fake
flag in your migrate command
./manage.py migrate app_name migration_number --fake
This will mark the migration as done.
- [Django]-Get a dropdown of states in Django Admin
- [Django]-Customize queryset in django-filter ModelChoiceFilter (select) and ModelMultipleChoiceFilter (multi-select) menus based on request
- [Django]-How do I load image to React from Django REST API?
- [Django]-Syntax error whenever I put Python code inside a Django template
- [Django]-Can I do Django & Python web development using Xcode 3.2?
Source:stackexchange.com