Jorge Vidinha
Jorge Vidinha

Reputation: 434

SQLAlchemy index names unique

Im building a table with index=True where i would like the name to always be a unique index name possible using UUID as suffix .. something like ix__uuid

Below my code for table creation

    add = Table('src_add_lookup', metadata,
                Column('id', Integer, Sequence('seq_add', schema='can'), primary_key=True, index=True)

metadata.drop_all(engine, tables=[add]) # using 'tables' param to only create the necessary table
metadata.create_all(engine, tables=[add]) 

This will always create an index named ix_src_add_lookup_id that i woudl like to avoid.

IS there some method available for something like this ?

Upvotes: 0

Views: 1499

Answers (1)

Jorge Vidinha
Jorge Vidinha

Reputation: 434

End up using the Index api together with uuid() lib to generate the desired unique index name like code below.

from sqlalchemy import Index

add = Table('src_add_lookup', metadata,
            Column('id', Integer, Sequence('seq_add', schema='can'), primary_key=True)

Index('idx_add_{}'.format(uuid.uuid4().hex), add.c.id, unique=True) 

Upvotes: 1

Related Questions