bit
bit

Reputation: 437

Some issues with UUID while migrating from SQLite to Postgresql in Django 2.0

I am trying to migrate my Django 2.0.4 project from SQLite to PostgreSQL 10 following the steps described here, but I am having differents problems. During the project I changed some Integer fields to UUID4 fields. I managed to run python manage.py migrate --run-syncdb manually editing auto_increment migration file making changes of this type (see id field): From

class Migration(migrations.Migration):

    dependencies = [
        ('dumps', '0011_auto_20180608_1714'),
    ]

    operations = [
        migrations.CreateModel(
            name='Report',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('data', models.DateTimeField(auto_now_add=True, verbose_name='Date')),
            ],
        ),
        ...
        ...
        ...

To

class Migration(migrations.Migration):

    dependencies = [
        ('dumps', '0011_auto_20180608_1714'),
    ]

    operations = [
        migrations.CreateModel(
            name='Report',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='ID')),
                ('data', models.DateTimeField(auto_now_add=True, verbose_name='Date')),
            ],
        ),
        ...
        ...
        ...

Next, I commented all auto_increment files in which there was an AlterTable on uuid fields, but when I run python manage.py loaddata datadump.json I obtain the following error:

django.db.utils.ProgrammingError: Problem installing fixture 'C:\Users\djangoproject\datadump.json': Could not load myApp.Reservation(pk=10d00b08-bf35-469f-b53f-ec28f8b6ecb3): ERROR:  column "reservation_id" is integer type but the expression is uuid type
LINE 1: UPDATE "myApp_reservation" SET "reservation_id" = '066cff3c-4b...

Upvotes: 0

Views: 1046

Answers (2)

bit
bit

Reputation: 437

I understood where the error was. The problem was in postgre table scheme: 'id' field had 'integer' type instead of 'uuid'. I converted it to 'uuid' and the import was successful.

Upvotes: 0

user10261970
user10261970

Reputation:

I think the issue here is that you have old migrations which refer to the int PK field column as an AutoField() before you made the change to use a UUIDField().

You may need to leave the id field as it was (perhaps reverse back your migrations to the point at which the swithc was made), and include a new field (and thus column of type uuid) named uuid in your Report model:

class Report(models.Model)

    id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, serialize=False, verbose_name='UUID')
    data = models.DateTimeField(auto_now_add=True, verbose_name='Date')
    ...

Then re-run database migrations ... you'll likely hit some more migration errors but give me a shout and I can advise on where to go from there in the chat.

Upvotes: 1

Related Questions