aachurin / peewee_migrations

Migrations for peewee orm.
Other
74 stars 8 forks source link

Issue with uuid as deafult value #36

Open henryh9n opened 5 months ago

henryh9n commented 5 months ago

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 commented 5 months ago

@aachurin Any ideas on this? Do you want me to go ahead and work on this?

aachurin commented 5 months ago

In the “general case” you can’t do this, do custom migrations