0

I am using a simple mixin for all my tables:

class IdMixIn:
    id = sa.Column(sa.Integer, primary_key=True, index=True)

Example table:

class MyTable(Base, IdMixIn):
    __tablename__ = 'my_table'

    my_col: str = sa.Column(sa.String)

I have about thirty of these tables, and if I run alembic revision --autogenerate I will then have thirty rows, one for each table of an index creation:

op.create_index(op.f('ix_my_table_id'), 'my_table', ['id'], unique=False)

However, in postgres, each of these already have an index, named something like my_table_pkey. How do I let alembic know these already exist for all the tables?

CasualDemon
  • 5,790
  • 2
  • 21
  • 39

1 Answers1

1

There is no need to create index on primary key column, so you can only write

class IdMixIn:
    id = sa.Column(sa.Integer, primary_key=True)

Postgresql will automatically create index for this column

puf
  • 359
  • 3
  • 13
  • 1
    So this answer doesn't actually solve the base question of "how do I tell alembic about existing indexing", but does help in particular here as it's what I need so thank you! If you happen to know also how to set a mixin to different index names on different tables, would be good to know. – CasualDemon Nov 08 '22 at 20:52