Re: [sqlalchemy] SQLAlchemy database record created on import of module

2021-03-15 Thread Simon King
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

On Fri, Mar 12, 2021 at 4:17 AM Advanced Spectrum
 wrote:
>
> 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?
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/7306a853-4017-4018-bc13-fa01aa8bb5adn%40googlegroups.com.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CAFHwexc6Wqeyf5w8CPsrm_jK_YJgV6au7UUQ-2fxxzHja6QQSg%40mail.gmail.com.


[sqlalchemy] SQLAlchemy database record created on import of module

2021-03-11 Thread Advanced Spectrum
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?

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/7306a853-4017-4018-bc13-fa01aa8bb5adn%40googlegroups.com.