sqlalchemy / sqlalchemy/alembic

implement autogen for PK constraints

Open
#993 5 comments 1 reaction 1 assignee View on GitHub

@CaselIT is already working on this.

Since Apr 24, 2026.

autogenerate - detection use case
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

Describe the bug
Given the following class:

class PersistedJobPartitionFile(Base):
    __tablename__ = "job_partition_files"

    job_id = Column(UUID(as_uuid=True), primary_key=True)
    partition_key = Column(String, primary_key=True)

    file_type = Column(SqlEnum(JobFileType), nullable=False, primary_key=True)

    path = Column(String, nullable=False)

I did migrations as normal, generating this line:
sa.PrimaryKeyConstraint('job_id', 'partition_key', 'file_type')

Then, I expanded the PK to include the path column:

class PersistedJobPartitionFile(Base):
    __tablename__ = "job_partition_files"

    job_id = Column(UUID(as_uuid=True), primary_key=True)
    partition_key = Column(String, primary_key=True)

    file_type = Column(SqlEnum(JobFileType), nullable=False, primary_key=True)

    # some partitions can have multiple files :/
    # so add the path to the PK
    path = Column(String, nullable=False, primary_key=True)

When autogenerating a migration, it simply creates an empty migration

Expected behavior
I expected an alter statement to be generated to adjust the PK.

Versions.

  • OS: OS x
  • Python: 3.7.9
  • Alembic: 1.7.6
  • SQLAlchemy: 1.4.31
  • Database: Postgres 14
  • DBAPI: psycopg2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.