Search code examples
pythonpython-3.xpeewee

Peewee incrementing an integer field without the use of primary key during migration


I have a table I need to add columns to it, one of them is a column that dictates business logic. So think of it as a "priority" column, and it has to be unique and a integer field. It cannot be the primary key but it is unique for business logic purposes.

I've searched the docs but I can't find a way to add the column and add default (say starting from 1) values and auto increment them without setting this as a primarykey..

Thus creating the field like

example_column = IntegerField(null=False, db_column='PriorityQueue',default=1)

This will fail because of the unique constraint. I should also mention this is happening when I'm migrating the table (existing data will all receive a value of '1')

So, is it possible to do the above somehow and get the column to auto increment?


Solution

  • It should definitely be possible, especially outside of peewee. You can definitely make a counter that starts at 1 and increments to the stop and at the interval of your choice with range(). You can then write each incremented variable to the desired field in each row as you iterate through.