pythonflask-migrate

How to add a new column after another existing column in flask-migrate?


flask-migrate detected my column changes and was able to successfully create and execute the migration.

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('vtuber_info', schema=None) as batch_op:
        batch_op.add_column(sa.Column('model', sa.VARCHAR(length=128), server_default='', nullable=False, comment='model info'))

However, I found that the new column was placed at the end of the table column, which is not good enough for viewing the data because I often use visual database management tools to view my data, such as Sequel Pro, and I have many data fields in a row, so I often need to scroll to the right to see the end of the table column data.

so do you have any suggestions?

BTW: My flask-migrate version is 4.0.4


Solution

  • This has little to do with flask or flask-migrate. Most database engines will add a column to the end of the table. One option is to rename the table, create a new table with columns in the order you want, and then copy data from the old table to the new table. As you can imagine this will take more time than just adding a single column.

    Another option is to see if your visual database tool allows you to reorder the columns in the UI.

    If neither of these work, then maybe you can create a view in the database with columns in the order you want.