0

I am confused as to how to drop entire columns on a Flask server using SQLAlchemy. I changed a variable name in one of my models, and after running $ flask db migrate and $ flask db upgrade the associated table now has two columns: one with the old name and one with the new name. My first part of the question is, how can I, at that point, change the column name when I have these two columns in the table? I have tried following the advice in this similar question's answer by modifying the script.py.mako file in my /migrations folder:

op.alter_column('table_name', 'old_col', nullable=False, new_column_name='new_col')

with no luck. I also tried using the batch_mode version of this code.

To make things worse, somewhere in the process I accidentally added data to this new column and now have two columns. So presently, I'd like to merge these two (over the NULL records), but haven't been able to find any relevant solutions to this.

I'd like to understand the answers to both situations should they arise again.

Brian Barry
  • 439
  • 2
  • 17

1 Answers1

1

I have found how to call op commands when upgrading. I have been editing the scripts.py.mako file when in fact I should've been editing the latest migration script, found in migrations/versions. To make changes to the table, I just needed to add the op.<sql_command> in the upgrade() function. So to drop columns I just inserted

with op.batch_alter_table("project") as batch_op:
    batch_op.drop_column('descr')

To avoid the shenanigans from the beginning that occurred from changing variable names all I had to do was: op.alter_column('project', 'descri', new_column_name='descr')

That said I still haven't figured out how to merge.

Brian Barry
  • 439
  • 2
  • 17