from operator import itemgetter
from alembic.autogenerate import rewriter
from alembic.operations.ops import CreateTableOp
from sqlalchemy.sql.schema import Column
writer = rewriter.Rewriter()
@writer.rewriter(CreateTableOp)
def order_columns(context, revision, op):
"""Reorder the columns before creating a table."""
preordered = []
for col in op.columns:
k = 0 # key used for ordering later on
if not isinstance(col, Column):
k = 99 # All constraints or indexes should stay at the end of the definition
elif col.primary_key and col.name=='id':
k = 1
# + a lot of other ordering constraints for other columns
else:
k= 2 # All non-id-columns
preordered.append((k, col)) # Creating my ordered list
# Now sorting the list and extracting only the column-objects.
# This list is indeed correctly sorted, just what I want
ordered_column_list = [itm[1] for itm in sorted(preordered, key=itemgetter(0))]
# Creating a new operation and returning it is not working, as it results in an error:
# Returning: ArgumentError: Column object '...' already assigned to Table '...'
# new_op = CreateTableOp(op.table_name, ordered_column_list, schema=op.schema)
# return new_op
# Reassigning the ordered column list is not working either, it seems to be ignored:
op.columns = ordered_column_list
return op
[...]
def run_migrations_online():
[...]
with connectable.connect() as connection:
context.configure(
[...]
process_revision_directives=writer
)
[...]
Hello,first and foremost: thank you for SQLAlchemy and Alembic. I've worked with a lot of ORMs, but only with these two I feel very comfortable and I'm doing a lot of crazy stuff with it.The current problem that I have: I'm currently creating a lot of tables with a lot of mixins, as most of them have e.g. an id-column or columns for created_at-dates etc. However it seems that I can't control the order of the columns, resulting in e.g. the primary-key-id-column to show up in the middle of the other columns or the created_at-column at the beginning and the created_by-column at the end, especially when I autogenerate the versioning scripts.
But alembic has also a method in this case (as always ;-)). So I created a Rewriter-method for the CreateTableOps in the env.py-file, re-order the columns in the correct way and reassign this ordered list of columns to op.columns. Unfortunately this doesn't work. Somehow it either uses whatever is already the to_table()-method (?) or ... something else. So I tried to create a new operation in the rewriter with the ordered list and returned this operation instead. But then I get an the error: sqlalchemy.exc.ArgumentError: Column object 'id' already assigned to Table 'user'.
--You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy-alembic/ef49db6c-420c-49ec-a708-86ad5d874fc1%40googlegroups.com.
sa.UniqueConstraint("iso_name"),
sa.UniqueConstraint("iso_name", name=op.f("uq_country_iso_name")),
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsub...@googlegroups.com.
Hello Mike,and thank you very much for the solution. It is working flawlessly for ordering the columns.For others finding this thread: Mike created an entry in the Alembic cookbook.There is just one (minor) flaw, where I currently don't know how to fix it, but I can adjust the generated script manually:I'm using the naming schema for constraints and indexes as described in the documentation. While the names for e.g. foreign keys, indexes or primary key constraints are kept with your solution, UniqueConstraints are strangely duplicated, once with the provided name and once just with the column name. This happens just for UniqueConstraints. So you have e.g. something like the following in the generated script:sa.UniqueConstraint("iso_name"),
sa.UniqueConstraint("iso_name", name=op.f("uq_country_iso_name")),So "uq_country_iso_name" in this case is the name provided by the naming schema, while the first UniqueConstraint is generated as duplication.Maybe any hint on what I'm doing wrong? Or maybe a bug in the Column.copy-method (although the Column.constraints-set on the copied column seems to be empty; also the copied columns-list as in your example contains the correct UniqueConstraint with the correct name; so maybe it happens in the CreateTableOp)?
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy-alembic/b552b5d5-e5f2-4553-85be-9e87350aed92%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy-alembic/b552b5d5-e5f2-4553-85be-9e87350aed92%40googlegroups.com.