tortoise / aerich

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

Problem mit default values from Enums #250

Open l1f opened 2 years ago

l1f commented 2 years ago

The mysql migration that aerich generates for enums has a problem with defaults

This state here causes an error:

ALTER TABLE `station` ALTER COLUMN `connection` SET DEFAULT 'Connection.OFFLINE'

pymysql.err.OperationalError: (1067, "Invalid default value for 'connection'")

The model on which this is based on:

class Connection(Enum):
    ONLINE = "online"
    OFFLINE = "offline"

class Station(Base):
    ...
    connection: Connection = fields.CharEnumField(enum_type=Connection, default=Connection.OFFLINE)
    ...

After adjusting the alter table statement, upgrading the database to the latest migration works.

ALTER TABLE 'station' ALTER COLUMN 'connection' SET DEFAULT 'OFFLINE';
long2ice commented 1 year ago

What's the version? Seems not problem for that since of https://github.com/tortoise/aerich/blob/dev/aerich/ddl/__init__.py#L75

l1f commented 1 year ago

Sorry for the long reply time, I was on holiday. In the project I have here, I use version 0.6.3