aerich icon indicating copy to clipboard operation
aerich copied to clipboard

Aerich does not set defaults when migrating postgres databases

Open PythonCoderAS opened this issue 4 years ago • 3 comments

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.

PythonCoderAS avatar Aug 02 '21 00:08 PythonCoderAS

The default is a value or callable?

long2ice avatar Aug 02 '21 01:08 long2ice

The default is a string.

PythonCoderAS avatar Aug 02 '21 03:08 PythonCoderAS

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 '[]';

pawelkoston avatar Apr 09 '24 12:04 pawelkoston