Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:14:30+00:00 2026-06-07T13:14:30+00:00

In SQLAlchemy, I want to define a mixin that automatically creates an Index in

  • 0

In SQLAlchemy, I want to define a mixin that automatically creates an Index in inheriting tables.

Assuming that the inheriting table has a member list called ‘keys’, I want the mixin to create in the inheriting table a single multi-column Index over the columns listed in keys. However, the mixin doesn’t know what the keys are until the table is created!

How would I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:14:32+00:00Added an answer on June 7, 2026 at 1:14 pm

    Example:

    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.ext.declarative import declared_attr
    from sqlalchemy import MetaData, Column, Index, Integer
    
    metadata = MetaData()
    Base = declarative_base(metadata=metadata)
    
    
    class MyMixin(object):
    
        @declared_attr
        def __table_args__(cls):
            return (Index('test_idx_%s' % cls.__tablename__, *cls.INDEX),)
    
    
    class MyModel(MyMixin, Base):
        __tablename__ = 'atable'
        INDEX = ('a', 'b',)
        id = Column(Integer, primary_key=True)
        a = Column(Integer)
        b = Column(Integer)
        c = Column(Integer)
    
    
    if __name__ == '__main__':
        from sqlalchemy import create_engine
        engine = create_engine('sqlite:///', echo=True)
        metadata.bind = engine
        metadata.create_all() 
    

    Docs: Combining Table/Mapper Arguments from Multiple Mixins

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table in sqlalchemy 0.4 that with types.DateTime column: Column(dfield, types.DateTime, index=True) I
Is there a way in the SQLAlchemy class of a table to define/create triggers
I want to reflect a schema using SQLAlchemy's MetaData.reflect() method, so that I can
We're using SQLAlchemy declarative base and I have a method that I want isolate
I access a a postgres table using SQLAlchemy. I want a query to have
I create a table in database using sqlalchemy and now want to make a
I want to generate this query in sqlalchemy. The table 'demande' exists in the
I'm using SQLAlchemy 0.6.3 with PostgreSQL 8.4 on Debian squeeze. I want a table
My problem is that I want to retrieve both a list of measurements along
I want to map a Tag entity using declarative method with SQLAlchemy. A tag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.