Skip to content

autogenerate incorrectly identifies truncated index names as new (postgres) #647

@mcclurem

Description

@mcclurem

I have a postgres database using naming conventions for automated naming of constraints following the naming scheme:

        meta = MetaData(naming_convention={
            "ix": "ix_%(column_0_N_label)s",
            "uq": "uq_%(table_name)s_%(column_0_N_name)s",
            "ck": "ck_%(table_name)s_%(constraint_name)s",
            "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
            "pk": "pk_%(table_name)s"
        })

class VRM(Base):
    __tablename__   = "vrm"
    __table_args__  = (UniqueConstraint('positive_refdes', 'positive_net', 'negative_refdes', 'negative_net', 'testcase_id'), UniqueConstraint("nickname", "testcase_id", name="_unique_vrm_name_constraint"))
    id              = Column(Integer, primary_key=True)
    nickname        = Column(String(256))
    testcase_id     = Column(ForeignKey("test_case.id"), nullable=True)
    positive_refdes = Column(String(64), nullable=False)
    negative_refdes = Column(String(64), nullable=False)
    positive_net    = Column(String(256), nullable=False)
    negative_net    = Column(String(256), nullable=False)

If I generate my database with a create_all I get my constraint named uq_vrm_positive_refdes_positive_net_negative_refdes_neg_fe9c as expected.
However when I subsequently run:
alembic revision --autogenerate -m "some message"
I get the following output:

INFO  [alembic.autogenerate.compare] Detected added unique constraint 'uq_vrm_positive_refdes_positive_net_negative_refdes_negative_net_testcase_id' on '['positive_refdes', 'positive_net', 'negative_refdes', 'negative_net', 'testcase_id']'
INFO  [alembic.autogenerate.compare] Detected removed unique constraint 'uq_vrm_positive_refdes_positive_net_negative_refdes_neg_fe9c' on 'vrm'

The generated upgrade contains the lines:

op.create_unique_constraint(op.f('uq_vrm_positive_refdes_positive_net_negative_refdes_negative_net_testcase_id'), 'vrm', ['positive_refdes', 'positive_net', 'negative_refdes', 'negative_net', 'testcase_id'])
op.drop_constraint('uq_vrm_positive_refdes_positive_net_negative_refdes_neg_fe9c', 'vrm', type_='unique')

the migration then fails because the op.f results in the correct naming scheme of uq_vrm_positive_refdes_positive_net_negative_refdes_neg_fe9c and postgres errors out because the constraint already exists

describing the table in postgres I see:

"_unique_vrm_name_constraint" UNIQUE CONSTRAINT, btree (nickname, testcase_id)
"uq_vrm_positive_refdes_positive_net_negative_refdes_neg_fe9c" UNIQUE CONSTRAINT, btree (positive_refdes, positive_net, negative_refdes, negative_net, testcase_id)

sqlalchemy version: 1.3.13
alembic version: 1.3.3
postgres server: 12.1
psycopg2 version: 2.8.4

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions