Changes between Version 2 and Version 3 of Ticket #23408, comment 6


Ignore:
Timestamp:
Nov 25, 2014, 2:23:20 PM (10 years ago)
Author:
Michael Barr

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23408, comment 6

    v2 v3  
    5959
    6060If default is set to a callable, I believe that it is implied that the result should be dynamically generated in Python/Django. If the functionality is to stay as stated in the result of this ticket, I would have to hand-code this migration for each app that previously existed to first create a nullable UUIDField with no default, then create a data migration to add distinct UUIDs to the Company, then write a migrations.AlterField migration to set null=False. The alternative might be to consider creating a special case for the UUIDField to allow for non-duplicated UUIDs to be created during migrations.
     61
     62In the end, what the current resolution states is that we would have to hand-code the following migration to avoid the failure, which I feel violates the DRY principle, as that is why we allow for abstract models:
     63{{{
     64# -*- coding: utf-8 -*-
     65from __future__ import unicode_literals
     66
     67from django.db import models, migrations
     68import uuid
     69
     70def generate_uuid(apps, schema_editor):
     71    Company = apps.get_model('company', 'Company')
     72    for company in Company.objects.all().iterator():
     73        company.uuid = uuid.uuid4()
     74
     75        company.save()
     76
     77class Migration(migrations.Migration):
     78
     79    dependencies = [
     80        ('main', '0001_initial'),
     81    ]
     82
     83    operations = [
     84        migrations.AddField(
     85            model_name='company',
     86            name='uuid',
     87            field=models.UUIDField(editable=False, max_length=32, help_text='Universally unique identifier', null=True, verbose_name='UUID'),
     88            preserve_default=False,
     89        ),
     90        migrations.RunPython(
     91            generate_uuid,
     92        ),
     93        migrations.AlterField(
     94            model_name='company',
     95            name='uuid',
     96            field=models.UUIDField(editable=False, max_length=32, help_text='Universally unique identifier', unique=True, verbose_name='UUID'),
     97            preserve_default=True,
     98        )
     99    ]
     100
     101}}}
Back to Top