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


Ignore:
Timestamp:
Nov 25, 2014, 1:39:27 PM (10 years ago)
Author:
Michael Barr

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #23408, comment 6

    v1 v2  
    11Callables on properties for ModelFields are used for various reasons. One use case is to autocreate UUIDs (https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.UUIDField).
    22
    3 My Models:
     3'''My Models:'''
    44{{{
    55import uuid
     
    1616}}}
    1717
    18 The generated migration:
     18'''The generated migration:'''
    1919{{{
    2020# -*- coding: utf-8 -*-
     
    4242}}}
    4343
    44 Based on the [https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.UUIDField documentation], you should add the default=uuid.uuid4 on the models.UUIDField. However, you cannot do this with the current way migrations work. Django currently tries to generate the same UUID for each instance that is created. In this instance (Django 1.8 isn't released yet), you will run into issues. If 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.
     44'''Results:'''
     45{{{
     46django.db.utils.IntegrityError: could not create unique index "company_company_uuid_key"
     47DETAIL:  Key (uuid)=(1ba0e330-9786-4928-b89d-b1fbef72b9c1) is duplicated.
     48}}}
     49
     50Based on the [https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.UUIDField documentation], you should add the default=uuid.uuid4 on the models.UUIDField. However, you cannot do this with the current way migrations work. Django currently tries to generate the same UUID for each instance that is created. In this instance (Django 1.8 isn't released yet), you will run into issues:
     51{{{
     52import uuid
     53from django.db import models
     54
     55class MyUUIDModel(models.Model):
     56    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
     57    # other fields
     58}}}
     59
     60If 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.
Back to Top