Add column after another column

2019-06-19 17:21发布

问题:

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.

回答1:

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;