peewee_migrate icon indicating copy to clipboard operation
peewee_migrate copied to clipboard

Add support for multi-column index/unique

Open juanitodread opened this issue 8 years ago • 3 comments

I added a "multi-column index unique" definition according to the PeeWee documentation: http://docs.peewee-orm.com/en/latest/peewee/models.html#indexes-and-constraints but when I run peewee_migrate it doesn't generate the statements for the multi-column index.

I was taking a look at your code (auto.py) and looks like you are not considering that case. It would be possible to add support for multicolumn index/unique ?

Adding manually the sentence using migrator.add_index(model, *col_names, unique=False) works but this means that I have to manually maintain autogenerated scripts and the other one for multi column indexes.

This is my model:

class Dog(BaseModel):
    id = PrimaryKeyField()
    user = ForeignKeyField(User, to_field='id', related_name='dogs', db_column='user_id')
    name = CharField(null=False)
    description = CharField(null=False)

    class Meta:
        indexes = (
            (('user', 'name'), True),  # This should generated a unique constraint of those two columns
        )

juanitodread avatar May 30 '17 18:05 juanitodread

Same question

pch18 avatar May 09 '20 07:05 pch18

@pch18 If I'm not mistaken, it's what you are searching for: https://stackoverflow.com/a/48614198/2396907

@juanitodread You'll only need to get rid of unpacking operator * in *col_names, so tuple is preserved.

vintprox avatar Jun 15 '20 02:06 vintprox

If you are using SchemaManager manually, you need to call create_all instead of create_table. This was my mistake and could be yours too.

NicolasCaous avatar Jun 17 '20 00:06 NicolasCaous