#23408: Automaticly created migration calls callable for default only once
----------------------------+------------------------------------
Reporter: Harper04 | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.7
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Comment (by michaeljohnbarr):
I am sorry - I modified the wrong widow - wasn't supposed to submit the
main window.
Callables on properties for ModelFields are used for various reasons. One
use case is to autocreate uuids.
My Models:
{{{
import uuid
class AbstractUUIDModel(models.Model):
[...]
uuid = models.UUIDField(default=uuid.uuid4, editable=False,
max_length=32, help_text='Universally unique identifier', unique=True,
verbose_name='UUID')
class Meta:
abstract = True
class Company(AbstractUUIDModel):
pass
}}}
The generated migration:
{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import main.models
import uuid
class Migration(migrations.Migration):
dependencies = [
('main', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='company',
name='uuid',
field=models.UUIDField(default=uuid.uuid4, editable=False,
max_length=32, help_text='Universally unique identifier', unique=True,
verbose_name='UUID'),
preserve_default=True,
),
]
}}}
Based on the 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/23408#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.0676acb93b5b1921ea283ad107ee1507%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.