2

I have got basically this model (other fields left out):

class GiftBase(models.Model):
    """
        A base class for describing gift type items.

    """
    uuid = models.CharField(max_length=32, default=lambda: uuid.uuid4().hex, unique=True)

When I do

./manage.py schemamigration facebookapp --auto
./manage.py migrate facebookapp

I get this output

Running migrations for facebookapp:
 - Migrating forwards to 0053_auto__add_field_giftbase_uuid__add_field_purchase_uuid.
 > facebookapp:0053_auto__add_field_giftbase_uuid__add_field_purchase_uuid
FATAL ERROR - The following SQL query failed: ALTER TABLE "facebookapp_giftbase" ADD COLUMN "uuid" varchar(32) NOT NULL UNIQUE DEFAULT '8de722a747974db3aab4c1fdaa618f16';
The error was: could not create unique index "facebookapp_giftbase_uuid_key"
DETAIL:  Key (uuid)=(8de722a747974db3aab4c1fdaa618f16) is duplicated.

From this, I realise that the default value for uuid field is precalculated and used for each new entry and therefore it is not unique to each object. How can I get it to calculate the value for each object individually?

Magnus Teekivi
  • 473
  • 1
  • 7
  • 21

2 Answers2

2

This should work:

from uuid import uuid4

from django.db.models import Model, CharField


def get_default_uuid():
    return uuid4().hex


class GiftBase(Model):
    """
    A base class for describing gift type items.
    """
    uuid = CharField(max_length=32, default=get_default_uuid, unique=True)

Don't forget to check autogenerated migration. If there is still hardcoded value, try to put get_default_uuid directly to the migration file and then edit default.

If you are adding that column to an already existing table you need to provide values for already existing rows.

South will ask you for a one-off value but as far as I know there is only datetime available. If so, you could use utcnow.strftime and then run update with uuid4 as the part of migration.

Also, I would suggest to avoid using lambdas for field options like default and always check autogenerated migrations.

Ernest
  • 2,799
  • 12
  • 28
0

I got this fixed. I first created the fields with null=True and unique=False, then I programmatically provided values for uuid fields and then altered the fields to make them unique=True and used create_unique on them.

Here's the code:

# -*- coding: utf-8 -*-
from south.utils import datetime_utils as datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models

import uuid


class Migration(SchemaMigration):

    def forwards(self, orm):
        # Adding field 'Company.uuid'
        db.add_column(u'facebookapp_company', 'uuid',
                      self.gf('django.db.models.fields.CharField')(max_length=32, null=True),
                      keep_default=False)

        # Adding field 'GiftBase.uuid'
        db.add_column(u'facebookapp_giftbase', 'uuid',
                      self.gf('django.db.models.fields.CharField')(max_length=32, null=True),
                      keep_default=False)

        # Adding field 'Purchase.uuid'
        db.add_column(u'facebookapp_purchase', 'uuid',
                      self.gf('django.db.models.fields.CharField')(max_length=32, null=True),
                      keep_default=False)

        for model in (orm.Company, orm.GiftBase, orm.Purchase):
            for obj in model.objects.all():
                obj.uuid = uuid.uuid4().hex
                obj.save()

        db.alter_column(u'facebookapp_company', 'uuid',
                self.gf('django.db.models.fields.CharField')(max_length=32, unique=True, null=False))
        db.create_unique(u'facebookapp_company', ['uuid'])

        db.alter_column(u'facebookapp_giftbase', 'uuid',
                self.gf('django.db.models.fields.CharField')(max_length=32, unique=True, null=False))
        db.create_unique(u'facebookapp_giftbase', ['uuid'])

        db.alter_column(u'facebookapp_purchase', 'uuid',
                self.gf('django.db.models.fields.CharField')(max_length=32, unique=True, null=False))
        db.create_unique(u'facebookapp_purchase', ['uuid'])


    def backwards(self, orm):
        # Deleting field 'Company.uuid'
        db.delete_column(u'facebookapp_company', 'uuid')

        # Deleting field 'GiftBase.uuid'
        db.delete_column(u'facebookapp_giftbase', 'uuid')

        # Deleting field 'Purchase.uuid'
        db.delete_column(u'facebookapp_purchase', 'uuid')
Magnus Teekivi
  • 473
  • 1
  • 7
  • 21