tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
804 stars 90 forks source link

Adding new a CharEnumField to existing table throws OperationError upon migration #291

Open bhch opened 1 year ago

bhch commented 1 year ago

Steps to reproduce:

class MyModel(Model):
    name = fields.CharField(max_length=10)
$ aerich migrate && aerich upgrade
class Colors(Enum):
    blue = 'blue'
    green = 'green'

class MyModel(Model):
    name = fields.CharField(max_length=10)
    color = fields.CharEnumField(max_length=5, default=Colors.blue)  # new field
$ aerich migrate && aerich upgrade

This throws this error:

tortoise.exceptions.OperationalError: value too long for type character varying(5)

Possible causes of error

Looking at the generated migration statement, we can see that it's using the incorrect default value.

-- generated by aerich

ALTER TABLE "mymodel" ADD "color2" VARCHAR(5) NOT NULL  DEFAULT 'Colors.blue';