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

Aerich does not set defaults when migrating postgres databases #182

Open PythonCoderAS opened 2 years ago

PythonCoderAS commented 2 years ago

When I add a default to a column that did not have a default, aerich will not add the default. It will write ALTER TABLE "<tablename>" ALTER COLUMN "<columnname>" SET;, which not only does not contain the default but also is a syntax error.

long2ice commented 2 years ago

The default is a value or callable?

PythonCoderAS commented 2 years ago

The default is a string.

pawelkoston commented 2 months ago

It also a problem for JSONB fields.

some_field = fields.JSONField(    default=[], null=False  )

produces migration:

 ALTER TABLE "some_table" ADD "some_field" JSONB NOT NULL

which causes error:

 tortoise.exceptions.IntegrityError: column "some_field" of relation "some_table" contains null values

The correct migration should be:

 ALTER TABLE "some_table" ADD "some_field" JSONB NOT NULL DEFAULT '[]';