Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when trying to evolve composite keys #48

Open
Torvaney opened this issue Apr 22, 2020 · 3 comments
Open

Error when trying to evolve composite keys #48

Torvaney opened this issue Apr 22, 2020 · 3 comments

Comments

@Torvaney
Copy link

Torvaney commented Apr 22, 2020

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.

@keredson
Copy link
Owner

keredson commented Apr 22, 2020

Glad it's been useful!

What database are you hitting?

EDIT: NM, I see postgres...

@Torvaney
Copy link
Author

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

@keredson
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants