Skip to content

Possibility to add callback #917

Closed Answered by itsbekas
AdamMusa asked this question in Questions
Apr 30, 2024 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

Hey! I think you're looking for SQLAlchemy's ORM Events, namely Mapper Events. You'll likely find a lot more information if you search for information related to SQLAlchemy rather than SQLModel (which uses SQLAlchemy internally), but here's a small example showcasing how these events work:

from sqlmodel import Field, SQLModel, create_engine, Session
from sqlalchemy import event

class Hero(SQLModel, table=True):
    name: str = Field(primary_key=True)
    secret_name: str = Field()

# Option 1: Setting the event listener on the class
def insert_is_done(mapper, connection, target):
    print("I just created a new Hero!")

event.listen(Hero, "after_insert", insert_is_done)

# Option 2: Sett…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@AdamMusa
Comment options

@itsbekas
Comment options

Answer selected by AdamMusa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants