13

I am trying to forward migrate a model with existing data. The model has a new field with constraints unique=True and null=False. When I do

./manage.py schemamigration myapp --auto

South lets me specify a default value for the new field by asking:

Specify a one-off value to use for existing columns now

Usually I set this to None but since this field needs to be unique I was wondering if it is possible to pass South a unique value via:

 >>> import uuid; uuid.uuid1().hex[0:35]

This gives me an error message

! Invalid input: invalid syntax 

Any ideas if it is possible to pass South random unique default values when migrating via the commandline?

Thanks.

mzu
  • 759
  • 8
  • 20

5 Answers5

29

Unfortunately only the datetime module is available for use as a one-off value in a schemamigration.

However, you can achieve the same effect by splitting this up into three migrations:

  • add new field to the model without constraints (with null=True, unique=False)
  • use a datamigration to add the UUID to the new field
  • add the constraint on the new field (with null=False, unique=True)

Tutorial on data migrations: http://south.readthedocs.org/en/0.7.6/tutorial/part3.html#data-migrations

Derek Kwok
  • 12,768
  • 6
  • 38
  • 58
  • Manual from Django https://docs.djangoproject.com/en/2.1/howto/writing-migrations/#migrations-that-add-unique-fields – Anatoly E Nov 20 '18 at 15:25
7

In django 1.7+ you can do the following. It first adds the field with no indexing and no unique. It then assigns the unique values (I based them on the name and used slugify method which you need to create) and finally alters the field again to add index and unique attributes.

from django.db import migrations
import re
import django.contrib.postgres.fields
from common.utils import slugify
import django.core.validators


def set_slugs(apps, schema_editor):
    categories = apps.get_model("myapp", "Category").objects.all()
    for category in categories:
        category.slug = slugify(category.name)
        category.save()


class Migration(migrations.Migration):

    dependencies = [
        ('myapp', '0034_auto_20150906_1936'),
    ]

    operations = [
        migrations.AddField(
            model_name='category',
            name='slug',
            field=models.CharField(max_length=30, validators=[django.core.validators.MinLengthValidator(2), django.core.validators.RegexValidator(re.compile('^[0-9a-z-]+$'), 'Enter a valid slug.', 'invalid')], help_text='Required. 2 to 30 characters and can only contain a-z, 0-9, and the dash (-)', unique=False, db_index=False, null=True),
            preserve_default=False,
        ),
        migrations.RunPython(set_slugs),
        migrations.AlterField(
            model_name='category',
            name='slug',
            field=models.CharField(help_text='Required. 2 to 30 characters and can only contain a-z, 0-9, and the dash (-)', unique=True, max_length=30, db_index=True, validators=[django.core.validators.MinLengthValidator(2), django.core.validators.RegexValidator(re.compile('^[0-9a-z-]+$'), 'Enter a valid slug.', 'invalid')]),
        ),
    ]
Nour Wolf
  • 2,140
  • 25
  • 24
3

Here is the Django's official how-to on migrating unique fields.

Migrations that add unique fields
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 Applying a "plain" migration that adds a unique non-nullable field to a table
 with existing rows will raise an error because the value used to populate
 existing rows is generated only once, thus breaking the unique constraint.

 Therefore, the following steps should be taken. In this example, we'll add a
 non-nullable :class:`~django.db.models.UUIDField` with a default value. Modify
 the respective field according to your needs.

 * Add the field on your model with ``default=...`` and ``unique=True``
   arguments. In the example, we use ``uuid.uuid4`` for the default.

 * Run the :djadmin:`makemigrations` command.

 * Edit the created migration file.

   The generated migration class should look similar to this::

     class Migration(migrations.Migration):

         dependencies = [
             ('myapp', '0003_auto_20150129_1705'),
         ]

         operations = [
             migrations.AddField(
                 model_name='mymodel',
                 name='uuid',
                 field=models.UUIDField(max_length=32, unique=True, default=uuid.uuid4),
             ),
         ]

   You will need to make three changes:

   * Add a second :class:`~django.db.migrations.operations.AddField` operation
     copied from the generated one and change it to
     :class:`~django.db.migrations.operations.AlterField`.

   * On the first operation (``AddField``), change ``unique=True`` to
     ``null=True`` -- this will create the intermediary null field.

   * Between the two operations, add a
     :class:`~django.db.migrations.operations.RunPython` or
     :class:`~django.db.migrations.operations.RunSQL` operation to generate a
     unique value (UUID in the example) for each existing row.

   The resulting migration should look similar to this::

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

     from django.db import migrations, models
     import uuid

     def gen_uuid(apps, schema_editor):
         MyModel = apps.get_model('myapp', 'MyModel')
         for row in MyModel.objects.all():
             row.uuid = uuid.uuid4()
             row.save()

     class Migration(migrations.Migration):

         dependencies = [
             ('myapp', '0003_auto_20150129_1705'),
         ]

         operations = [
             migrations.AddField(
                 model_name='mymodel',
                 name='uuid',
                 field=models.UUIDField(default=uuid.uuid4, null=True),
             ),
             # omit reverse_code=... if you don't want the migration to be reversible.
             migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
             migrations.AlterField(
                 model_name='mymodel',
                 name='uuid',
                 field=models.UUIDField(default=uuid.uuid4, unique=True),
             ),
         ]
* Now you can apply the migration as usual with the :djadmin:`migrate` command.

   Note there is a race condition if you allow objects to be created while this
   migration is running. Objects created after the ``AddField`` and before
   ``RunPython`` will have their original ``uuid``’s overwritten.
Ashish Gupta
  • 2,574
  • 2
  • 29
  • 58
  • This worked for me, but I had to split the commands into 3 different migration files, otherwise PostGRES was throwing the error `cannot ALTER TABLE "app_modelname" because it has pending trigger events` – penguin Mar 15 '16 at 09:03
0

You can manually edit your migration file:

I needed to add random character to some field so I have imported random and randint

import random
import string

and changed the value of default to

default=random.choice(string.lowercase)

It worked.

Visgean Skeloru
  • 2,237
  • 1
  • 24
  • 33
  • 11
    This code just generates one random letter and sets it as default for all rows, without solving the uniqueness problem – Viorel Jul 11 '13 at 07:18
  • `import random; import string; ''.join([random.choice(string.ascii_letters + string.digits) for x in range(10)])` might be better? – agconti Apr 22 '14 at 20:54
  • no @agconti the OP asks for a unique value for each row – fjsj May 13 '14 at 19:33
  • 1
    This worked for me as well, I just entered a value to get the migration made. Then I edited the file directly to assign the value I wanted. In my case, a GUID. – Luke Dupin Oct 22 '14 at 08:00
0

There is way to do unique value for each row with South.

Define slug in models.py as:

class Foo(models.Model):
  slug = models.SlugField(unique=True, default='')
  ....

Create new migration

run python manage.py schemamigration --auto foo

Open new migration file, and edit it:

# Change add_column to this:
db.add_column(u'account_funnel', 'slug',
            self.gf('django.db.models.foo.Foo')(default='', 
                  unique=False,  
                  max_length=50),
            keep_default=False)

# right above this add such python code:
foos = orm['foo.Foo'].objects.all()
for foo in foos:
        foo.slug = slugify(funnel.name)
        foo.save()

# Modify slug as unique field
db.create_unique(u'foo_foo', ['slug'])

ps mark this migration as no_dry_run = True
pss do not forget to import slugify function from django.template.defaultfilters import slugify
Vlad
  • 947
  • 1
  • 10
  • 24