7

How can I add a column after another column to a database using Alembic or SQLAlchemy? That would be equivalent to this SQL clause:

ALTER TABLE foo
CHANGE COLUMN bar
bar COLUMN_DEFINITION_HERE
AFTER OTHER_COLUMN;
-- or
ALTER TABLE foo ADD COLUMN baz AFTER bar;

I also tried the suggestion in this mailing list thread, but it didn't help.

While the order doesn't matter when querying, it helps readability for large tables when in the SQL shell.

davidism
  • 121,510
  • 29
  • 395
  • 339
Hamed
  • 869
  • 10
  • 26

2 Answers2

-1

Try this query :

ALTER TABLE table_name ADD New_column_name column_definition AFTER column_name;

Purvi Barot
  • 241
  • 2
  • 9
-4

Use ALTER with AFTER to specify where to place the column.

ALTER TABLE foo 
CHANGE COLUMN foo1 foo1 INT(10) UNSIGNED NULL DEFAULT NULL AFTER foo2;
davidism
  • 121,510
  • 29
  • 395
  • 339
Ton
  • 316
  • 2
  • 12
  • Yes, that's the syntax the OP wants to *generate* using either of the SQLAlchemy schema migration management tools, so Alembic or SQLAlchemy-Migrate. Your answer doesn't cover that. – Martijn Pieters May 22 '19 at 11:43