django-smalluuid icon indicating copy to clipboard operation
django-smalluuid copied to clipboard

Add documentation for migrations

Open adamcharnock opened this issue 9 years ago • 0 comments

Should reflect this:

https://docs.djangoproject.com/en/1.10/howto/writing-migrations/#migrations-that-add-unique-fields

However, I had to do it along these lines:

class Migration(migrations.Migration):

    dependencies = [
        ('hordak', '0009_auto_20160910_1330'),
    ]

    operations = [
        migrations.AddField(
            model_name='account',
            name='uuid',
            field=django_smalluuid.models.SmallUUIDField(default=None, null=True, unique=False),
        ),
    ]

Note that the above migration explicitly sets no default and forces unique=False (unlike the example in the django docs)

def gen_uuid(apps, schema_editor):
        Model = apps.get_model('hordak', 'account')
        for row in Model.objects.all():
            row.uuid = uuid.uuid4()
            row.save()


class Migration(migrations.Migration):

    dependencies = [
        ('hordak', '0010_auto_20160912_2354'),
    ]

    operations = [
        migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
    ]
# -*- coding: utf-8 -*-
# Generated by Django 1.10.1 on 2016-09-13 00:05
from __future__ import unicode_literals

import django_smalluuid
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('hordak', '0011_auto_20160913_0002'),
    ]

    operations = [
        migrations.AlterField(
            model_name='account',
            name='uuid',
            field=django_smalluuid.models.SmallUUIDField(default=django_smalluuid.models.UUIDDefault(), null=False, unique=True),
        )
    ]

adamcharnock avatar Sep 13 '16 00:09 adamcharnock