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 | {{{ |
| 46 | django.db.utils.IntegrityError: could not create unique index "company_company_uuid_key" |
| 47 | DETAIL: Key (uuid)=(1ba0e330-9786-4928-b89d-b1fbef72b9c1) is duplicated. |
| 48 | }}} |
| 49 | |
| 50 | 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: |
| 51 | {{{ |
| 52 | import uuid |
| 53 | from django.db import models |
| 54 | |
| 55 | class MyUUIDModel(models.Model): |
| 56 | id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) |
| 57 | # other fields |
| 58 | }}} |
| 59 | |
| 60 | 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. |