peewee-db-evolve icon indicating copy to clipboard operation
peewee-db-evolve copied to clipboard

Error when trying to evolve composite keys

Open Torvaney opened this issue 5 years ago • 3 comments
trafficstars

Hello,

I really like this package and have used it in many different projects with no issues, so first of all I would like to thank you for developing and sharing it.

I recently came across an issue where a composite primary key was causing an exception when attempting to evolve, despite having made no changes to the relevant class.

The relevant part of the schema looks something like this:

import peewee
import peeweedbevolve

import config

DB = peewee.PostgresqlDatabase(**config.DATABASE)


class DBModel(peewee.Model):
    class Meta:
        database = DB
        legacy_table_names = False


class MyTable(DBModel):
    x = peewee.IntegerField(index=True)
    y = peewee.IntegerField(index=True)

    class Meta:
        primary_key = peewee.CompositeKey('x', 'y')

if __name__ == "__main__":
    DB.evolve(ignore_tables=['db_model'])

and the exception:

Exception: In table 'my_table' I don't know how to change ColumnMetadata(name='x', data_type='integer', null=False, primary_key=True, table='my_table', default=None, max_length=None, precision=None, scale=None) into ColumnMetadata(name='x', data_type='integer', null=False, primary_key=False, table='my_table', default=None, max_length=None, precision=None, scale=None)

I have found a satisfactory work-around by altering MyTable to use a standard primary key, and a separate uniqueness constraint:

class MyTable(DBModel):
    id = peewee.PrimaryKeyField()
    x = peewee.IntegerField(index=True)
    y = peewee.IntegerField(index=True)

    class Meta:
        indexes = (
            (('x', 'y'), True),
        )

However, it would be nice if evolving composite primary keys worked without issue, too.

Torvaney avatar Apr 22 '20 20:04 Torvaney

Glad it's been useful!

What database are you hitting?

EDIT: NM, I see postgres...

keredson avatar Apr 22 '20 20:04 keredson

SELECT version();
-- PostgreSQL 11.3 on x86_64-apple-darwin18.5.0, compiled by Apple LLVM version 10.0.1 (clang-1001.0.46.4), 64-bit

Torvaney avatar Apr 22 '20 20:04 Torvaney

the above change (released in v3.7.4) will stop the error when evolving an unchanged composite key.

i've not implemented any logic for evolving composite keys, so going to leave this issue open to eventually implement that. open to volunteers!

keredson avatar Apr 22 '20 21:04 keredson