SQLAlchemy database record created on import of module

9 views
Skip to first unread message

Advanced Spectrum

unread,
Mar 11, 2021, 11:17:50 PM3/11/21
to sqlalchemy
I have a Base class like this:

class FlagTable(Base):
    
    __tablename__ = "FLAG_TABLE"

    FLAG_TABLE_ID = Column(Integer, primary_key=True, autoincrement="auto")
    COLUMN_1 = Column(Boolean)
    COLUMN_2 = Column(Boolean)
    COLUMN_3 = Column(Boolean)

I have a Python function in 'FlagTable.py' that would create a record with the ORM FlagTable object:

from FlagTable import FlagTable

def addFlagsToFlagTable(arg1, arg2, arg3):

        myFlags= FlagTable(COLUMN_1=arg1, COLUMN_2=arg1, COLUMN_3=arg1)
        session.add(myFlags)
        session.commit()

#main code starts here
callFunc = addFlagsToFlagTable(true, false, false)


When I step through the code in debugger mode I see that the record I want to add to my postgres database table gets added at the time of the import. It does not even wait for when the addFlagsToFlagTable function gets called in my main code. My *'Base = declarative_base()'* statement is in another class which is related to FlagTable class. Can someone educate me a bit on what is going on?

Simon King

unread,
Mar 15, 2021, 6:22:45 AM3/15/21
to sqlal...@googlegroups.com
I suggest you set up an event listener for the "after_attach" event on
your session:

https://docs.sqlalchemy.org/en/13/orm/events.html#sqlalchemy.orm.events.SessionEvents.after_attach

Then you can set a breakpoint in the listener (or raise an exception,
or use the traceback module to print a stack trace) to find out where
in your code you are adding this object to the session.

Hope that helps,

Simon
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/7306a853-4017-4018-bc13-fa01aa8bb5adn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages