aerich icon indicating copy to clipboard operation
aerich copied to clipboard

Adding new a CharEnumField to existing table throws OperationError upon migration

Open bhch opened this issue 2 years ago • 0 comments

Steps to reproduce:

  • Step 1: Create model
class MyModel(Model):
    name = fields.CharField(max_length=10)
  • Step 2: Create migration and upgrade
$ aerich migrate && aerich upgrade
  • Step 3: Add a new enum field on the model
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
  • Step 4: Migrate and upgrade
$ 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';

bhch avatar Mar 18 '23 09:03 bhch