Opened 6 years ago

Last modified 6 years ago

#30431 closed Bug

Migration hangs when using UUIDField with unique default value & multiple databases — at Initial Version

Reported by: mandm Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords: migrations, uuid, uuidfield, database router, multiple database
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm trying to add a UUIDField in an existing user model following this method (with unique=True & default=uuid.uuid4). https://docs.djangoproject.com/en/1.11/howto/writing-migrations/#migrations-that-add-unique-fields

This works with a single database but hangs in the following scenarios with multiple databases :

  • Migration without --database=foobar works
  • Migration with --database=foobar option hangs
  • Migration without --database=foobar, but with using('foobar') in the query in gen_uuid function hangs

Here's a snippet of the code I've used. I would be glad to provide additional information to reproduce the issue.

User model :

class CustomUser(AbstractUser):
    address = models.TextField()
    uniq_id = models.UUIDField(unique=True, default=uuid.uuid4) 

    class Meta:        
        db_table = 'customuser'

Migration file

def gen_uuid(apps, schema_editor):
    UserModel = apps.get_model('myapp', 'customuser')
    for row in UserModel.objects.all():     # .objects.using('foobar').all(): -- HANGS ! 
        row.uniq_id = uuid.uuid4()
        row.save(update_fields=['uniq_id']) # .save(using='foobar', ...)      -- HANGS with database routers ! 

class Migration(migrations.Migration):
    dependencies = [
        ('myapp', '003_previous_migration'),
    ]

    operations = [
        migrations.AddField(
            model_name='customuser',
            name='uniq_id',
            field=models.UUIDField(default=uuid.uuid4, null=True),
        ),
        migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop),
        migrations.AlterField(
            model_name='customuser',
            name='uniq_id',
            field=models.UUIDField(default=uuid.uuid4, unique=True),
        ),
    ]

Change History (0)

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