Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

alembic --auogenerate should put columns from base class before columns from derived class #1318

Closed
BlackZork opened this issue Oct 4, 2023 · 2 comments

Comments

@BlackZork
Copy link

Describe the bug

@dataclass
class DbObject:
    id: Mapped[int] = mapped_column(primary_key=True)

@mapper_registry.mapped_as_dataclass
class IrsUser(DbObject):
    __tablename__ = "users"
    email: Mapped[str]

Result of alembic revision --autogenerate:

    op.create_table('users',
    sa.Column('email', sa.String(), nullable=False),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )

Expected behavior
In most cases it should be in reverse order. Columns from base class should be added first, then columns from derived classes.

Versions:
python 3.9
sqlalchemy 2.0.19
alembic 1.8,1

Have a nice day!

@BlackZork BlackZork added the requires triage New issue that requires categorization label Oct 4, 2023
@CaselIT CaselIT added expected behavior and removed requires triage New issue that requires categorization labels Oct 4, 2023
@CaselIT
Copy link
Member

CaselIT commented Oct 4, 2023

Hi,

This behaviour comes from sqlalchemy, and it's unlikely to be changed, at least not in a minor version.

You can use sort_order to control how the columns are laid out in a table https://docs.sqlalchemy.org/en/20/orm/mapping_api.html#sqlalchemy.orm.mapped_column.params.sort_order

@CaselIT CaselIT closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2023
@BlackZork
Copy link
Author

Many thanks, I did not know about sort order. This works:

@dataclass
class DbObject:
    id: Mapped[int] = mapped_column(primary_key=True, sort_order=-99)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants