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

  • Home
  • SEARCH
  • 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 7084945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:21:52+00:00 2026-05-28T07:21:52+00:00

Is there a way in the SQLAlchemy class of a table to define/create triggers

  • 0

Is there a way in the SQLAlchemy class of a table to define/create triggers and indexes for that table?

For instance if i had a basic table like …

class Customer(DeclarativeBase):
    __tablename__ = 'customers'
    customer_id = Column(Integer, primary_key=True,autoincrement=True)
    customer_code = Column(Unicode(15),unique=True)
    customer_name = Column(Unicode(100))
    search_vector = Column(tsvector) ## *Not sure how do this yet either in sqlalchemy*.

I now want to create a trigger to update “search_vector”

CREATE TRIGGER customers_search_vector_update BEFORE INSERT OR UPDATE
ON customers
FOR EACH ROW EXECUTE PROCEDURE
tsvector_update_trigger(search_vector,'pg_catalog.english',customer_code,customer_name);

Then I wanted to add that field also as an index …

create index customers_search_vector_indx ON customers USING gin(search_vector);

Right now after i do any kind of database regeneration from my app i have to do the add column for the tsvector column, the trigger definition, and then the index statement from psql. Not the end of the world but its easy to forget a step. I am all about automation so if I can get this all to happen during the apps setup then bonus!

  • 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-05-28T07:21:52+00:00Added an answer on May 28, 2026 at 7:21 am

    Indicies are straight-forward to create. For single-column with index=True parameter like below:

    customer_code = Column(Unicode(15),unique=True,index=True)
    

    But if you want more control over the name and options, use the explicit Index() construct:

    Index('customers_search_vector_indx', Customer.__table__.c.search_vector, postgresql_using='gin')
    

    Triggers can be created as well, but those need to still be SQL-based and hooked to the DDL events. See Customizing DDL for more info, but the code might look similar to this:

    from sqlalchemy import event, DDL
    trig_ddl = DDL("""
        CREATE TRIGGER customers_search_vector_update BEFORE INSERT OR UPDATE
        ON customers
        FOR EACH ROW EXECUTE PROCEDURE
        tsvector_update_trigger(search_vector,'pg_catalog.english',customer_code,customer_name);
    """)
    tbl = Customer.__table__
    event.listen(tbl, 'after_create', trig_ddl.execute_if(dialect='postgresql'))
    

    Sidenote: I do not know how to configure tsvector datatype: deserves a separate question.

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

Sidebar

Related Questions

is there a way how to fully load some SQLAlchemy ORM mapped instance (together
I'm wondering if there's a way to create a case statement with SqlAlchemy, e.g.
is there way how to get name ov event from Lambda expression like with
Is there a way to find the name of the program that is running
In SQLAlchemy, we can declare tables and their relations like this: user = Table(
I have an SqlAlchemy table like so: table = sql.Table('treeItems', META, sql.Column('id', sql.Integer(), primary_key=True),
Is there a way in sqlalchemy to turn off declarative's polymorphic join loading, in
Is there way in next piece of code to only get the first record?
is there way thats i can preselect an item when the page loads or
Is there way to better identify design pattern in source codes, esp. if you

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.