peewee_migrations icon indicating copy to clipboard operation
peewee_migrations copied to clipboard

Issue with uuid as deafult value

Open henryh9n opened this issue 4 months ago • 2 comments

Ussing uuid as a default value for a field results in the following migration:

class Student(peewee.Model):
    ...
    uuid = UUIDField(default=uuid.uuid4, unique=True)
    class Meta:
        table_name = "student"


def forward(old_orm, new_orm):
    student = new_orm['student']
    return [
        # Apply default value UUID('e942f2c0-9fd8-4d1e-86ac-5fae12451163') to the field student.uuid,
        student.update({student.uuid: UUID('e942f2c0-9fd8-4d1e-86ac-5fae12451163')}).where(student.uuid.is_null(True)),
    ]

There's two issues here:

  1. it uses the UUID object, which is not imported
  2. instead of calling the uuid4 function to generate a random uuid for each entry, it uses a single one for all of those, causing an error with Unique constraint

henryh9n avatar Mar 24 '24 23:03 henryh9n