peewee_migrate
peewee_migrate copied to clipboard
Add support for multi-column index/unique
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
)
Same question
@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.
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.