state_operations while adding 2 fields

5 views
Skip to first unread message

Axel Rau

unread,
May 31, 2019, 7:15:39 AM5/31/19
to Django users
Hi all,

is it posible to bundle 2 AddField migrations with one RunPython?
How should I arrange the state_operations in this case?

The following migration does not work.

Please advice,
Axel

def forwards_func(apps, schema_editor):
with connections['erdb_migration'].cursor() as cursor:
cursor.execute('Alter TABLE account \
ADD column is_account_admin boolean \
DEFAULT false')
cursor.execute('Alter TABLE account \
ADD column self_admin_only boolean \
DEFAULT false')

def reverse_func(apps, schema_editor):
with connections['erdb_migration'].cursor() as cursor:
cursor.execute('Alter TABLE account \
DROP column is_account_admin')
cursor.execute('Alter TABLE account \
DROP column self_admin_only')


class Migration(migrations.Migration):

dependencies = [
('erdb', '0007_add_customer_group'),
]


operations = [
migrations.RunPython(forwards_func, reverse_code=reverse_func),
]

state_operations=[(
migrations.AddField(
model_name='account',
name='is_account_admin',
field=models.BooleanField(
db_column='is_account_admin',
default=False),
),
migrations.RemoveField(
model_name='account',
name='is_account_admin'
)),(
migrations.AddField(
model_name='account',
name='self_admin_only',
field=models.BooleanField(
db_column='self_admin_only',
default=False),
),
migrations.RemoveField(
model_name='account',
name='self_admin_only'
))

],

---
PGP-Key:29E99DD6 ☀ computing @ chaos claudius

Axel Rau

unread,
May 31, 2019, 10:06:17 AM5/31/19
to Django users


> Am 31.05.2019 um 13:15 schrieb Axel Rau <Axel...@chaos1.de>:
>
> is it posible to bundle 2 AddField migrations with one RunPython?
> How should I arrange the state_operations in this case?
>
> The following migration does not work.

It seems, the variant below does the job.
However there is no state operation for undoing the migration.

Axel

- - -
class Migration(migrations.Migration):

dependencies = [
('erdb', '0007_add_customer_group'),
]

operations = [
migrations.RunPython(forwards_func, reverse_code=reverse_func),
migrations.SeparateDatabaseAndState(
database_operations=[],
state_operations=[
migrations.AddField(
model_name='account',
name='is_account_admin',
field=models.BooleanField(
db_column='is_account_admin',
default=False),
),
migrations.AddField(
model_name='account',
name='self_admin_only',
field=models.BooleanField(
db_column='self_admin_only',
default=False),
),
],
)
]
- - -
Reply all
Reply to author
Forward
0 new messages