Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#23932 closed Cleanup/optimization (fixed)

Document how to set a unique value for new fields using migrations

Reported by: Michael Barr Owned by: andrei kulakov
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: info+coding@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no


This ticket questions two items: the models.UUIDField documentation, and the action of a callable default on a model field for migrations.

The current documentation for a models.UUIDField states to have a default set to default=uuid.uuid4.
Assuming I originally had the following model:

from django.db import models
class TestUUID(models.Model):
    name = models.CharField(max_length=15)

Then I run a makemigrations and a migrate to create the model.

I then run:

from testapp.models import TestUUID
TestUUID.objects.create(name='Test 1')
TestUUID.objects.create(name='Test 2')
TestUUID.objects.create(name='Test 3')
TestUUID.objects.create(name='Test 4')
TestUUID.objects.create(name='Test 5')

I decide to add a models.UUIDField to the model:

from django.db import models
import uuid
class TestUUID(models.Model):
    name = models.CharField(max_length=15)
    uuid = models.UUIDField(default=uuid.uuid4, unique=True)

I run makemigrations, which generates:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import uuid

class Migration(migrations.Migration):

    dependencies = [
        ('testapp', '0001_initial'),

    operations = [
            field=models.UUIDField(default=uuid.uuid4, unique=True, max_length=32),

When I run migrate, I get the following error:

  Applying testapp.0002_testuuid_uuid...Traceback (most recent call last):
  File "", line 10, in <module>
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/core/management/", line 338, in execute_from_command_line
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/core/management/", line 330, in execute
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/core/management/", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/core/management/", line 442, in execute
    output = self.handle(*args, **options)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/core/management/commands/", line 193, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/migrations/", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/migrations/", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/migrations/", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/migrations/operations/", line 51, in database_forwards
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/backends/sqlite3/", line 167, in add_field
    self._remake_table(model, create_fields=[field])
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/backends/sqlite3/", line 135, in _remake_table
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/backends/", line 100, in execute
    cursor.execute(sql, params)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/backends/", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/backends/", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/backends/", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/michaelbarr/.virtualenvs/django_18_venv/src/django/django/db/backends/sqlite3/", line 518, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: column uuid is not unique

I have seen the response to Automaticly created migration calls callable for default only once, which states:

I wouldn't consider this a bug as the default value is nothing that is enforced on database level but inside Django. If you want to have random values per row, you need to add a ​RunPython operation that updates the value per row.

This is exactly as designed; when creating columns databases only take a single argument for the DEFAULT clause, so we can only provide a single value, so we only call a callable once. If you want unique values per row, you'll need to populate them in RunPython as Markush2010 suggests.

The new UUID field does not allow to automatically generate a UUID on save also discusses this issue, talking of the possibility of auto_add and auto_add_now.

It is my personal belief that if a developer sets the model field's default value to be a callable, it is meant to be handled by Python, not the database. In my instance above, I am stating that I want a new uuid.uuid4 to be created as the default. This works GREAT for forms and new saves, but not for the migrations since they are all the same uuid.uuid4(), as the migrations casts the default value to the called value. From what I can tell, I am not the only person that is confused by the defaults. In Python, the defaults work as expected, but database migrations do not yield the same results.

What I am attempting to do (eventually) is create an abstract model which is reused in multiple applications as a convention. However, this model has existed for quite some time, so each application already has multiple models created. I feel that the models.UUIDField should consider correcting the documentation or making a note of it, or reconsider how migration defaults are handled as a whole. I wrote about this as a comment here regarding how much work it is to work around this issue. If that is to be the case, so be it, but please be sure to make this clear in the documentation.

Change History (9)

comment:1 by Markus Holtermann, 9 years ago

Cc: info+coding@… added
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

As stated in #23408, the handling in migrations was a design decision that I still consider to be the right way to do. The normal cases are non-unique columns that are added with a default value equal for every existing row.

What I do agree with, is explaining how to set a default value for a unique field:

  • AlterField / CreateField with unique=True
  • RunPython / RunSQL to propagate existing rows
  • AlterField to default=uuid.uuid4

comment:2 by Shai Berger, 9 years ago

(nitpicking) A more general recipe would be:

  • AlterField / CreateField with unique=False, null=True
  • RunPython / RunSQL to propagate existing rows
  • AlterField to null=False, unique=True and in the case of UUID default=uuid.uuid4

comment:3 by Michael Barr, 9 years ago

Excellent points. Another talking point that may be helpful is how one might handle inheritance with migrations (such as creating a base migration). Assuming we have an abstract model that can be extended, what would be the suggested standard to handle the migration for new/existing models? It is difficult to know which fields already exist on the models, and which fields don't. Thus, it appears that each migration for each app would have to be hand-coded.

Thank you for your consideration, responses, and clarification.

comment:4 by Tim Graham, 9 years ago

Component: MigrationsDocumentation
Summary: models.UUIDField() documentation / migrationsDocument how to set a unique value for new fields using migrations

comment:5 by andrei kulakov, 9 years ago

Owner: changed from nobody to andrei kulakov
Status: newassigned

comment:6 by Tim Graham, 9 years ago

Has patch: set

comment:7 by Tim Graham, 9 years ago

Patch needs improvement: set

comment:8 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 1f9e44030e9c5300b97ef7b029f482c53a66f13b:

Fixed #23932 -- Added how-to on migrating unique fields.

comment:9 by Loic Bistuer <loic.bistuer@…>, 9 years ago

In 564487601e34b9962ad16bc7e4e62c8b68928c84:

[1.8.x] Fixed #23932 -- Added how-to on migrating unique fields.

Backport of 1f9e44030e9c5300b97ef7b029f482c53a66f13b from master

Note: See TracTickets for help on using tickets.
Back to Top