0

I am using SQLAlchemy and sqlalchemy-migrate for my project.
Now, I want to add a column to already created table, and I want that column to be at a specific position in the table. What I basically want, is to use:

ALTER table table_name add column column_name2 integer AFTER column_name1

but using sqlalchemy-migrate API.
I tried to search documentation and read sources, but got no result.
Does this functionality even exist, and if so, how can it be used?

Eugene
  • 126
  • 7
  • I don't think either Alembic or SQLAlchemy-Migrate support adding columns at a specific location out of the box. – Martijn Pieters May 22 '19 at 11:42
  • @MartijnPieters I suppose, that is an issue with Alchemy itself, not the migrate tools designed for it? – Eugene May 22 '19 at 11:43
  • No, it is an issue with the migration tools. SQLAlchemy can be taught how to emit that syntax with `@compile` rules, see https://docs.sqlalchemy.org/en/latest/core/compiler.html#changing-the-default-compilation-of-existing-constructs – Martijn Pieters May 22 '19 at 11:47
  • And as [Michael Bayer (SQLAlchemy and Alembic author) states](https://groups.google.com/d/msg/sqlalchemy-alembic/izYq2EMYotI/DDjtj-HC_joJ), Alembic already uses `@compiles` to produce its output, so changing those is a little tricky but can be achieved. – Martijn Pieters May 22 '19 at 11:49
  • @MartijnPieters well, thank you for the suggestion. Weird that this is not yet implemented in those tools, they seem pretty popular. Sorry for the duplicate, somehow both duplicates didn't show up in search results. Anyway, thanks. – Eugene May 22 '19 at 11:50
  • Column order in tables is purely cosmetic, really. It may help document the table better in tools that show the table column order from the schema, but when working with SQLAlchemy column order really doesn't matter. So it's not all that great a need to have to manage the ordering. – Martijn Pieters May 22 '19 at 11:58
  • @MartijnPieters well, I've met enough tools outside pythonland that implement this functionality. For me, primary reason to be looking for this is readability. I am working a lot with the database using cli, heidisql and similar tools. And having to scroll all the way right for that very-important-column-i-added-later every time seems a bit unproductive. – Eugene May 22 '19 at 12:52

0 Answers0