Re: [sqlalchemy] Re: Do autoflushes occur asynchronously?
On Wed, Jan 16, 2019 at 4:34 PM Gmoney wrote: > > It appears that > > flush_context.execute() > > > Is the step that determines the FK id of the relationship object and sets > that FK id value on the base object. But then it wipes the session's > attribute history, as it's been flushed to the DB. Wondering if there's a > way to trigger this operation without flushing, so I can interrogate the > object and see that that FK id was changed (has a history). not unless you use event hooks to inspect things as they change. Feel free to share details regarding what you actually need to accomplish and why. > > > On Wednesday, January 16, 2019 at 4:18:36 PM UTC-5, Gmoney wrote: >> >> I was having a heck of confusing time debugging some code where I was trying >> to determine what attrs on an ORM changed prior to committing it. Every >> time in my debugger I'd be getting different results (w/r to History). I >> finally realized that autoflush was enabled, and now that I disabled it, >> things seem much more sane. The strange part though, is that the >> update/flush would occur randomly while I was just iterating through some >> mapper attributes checking History. No queries, no nothing... but at one >> point... whoosh... it must have flushed. >> >> I'm basing the 'has flushed' inference on the fact that my test update is >> updating a foreign key relationship. I update the relationship object (not >> the ID) on the primary object. At that point, the FK id is still the old id >> from prior to the change. Only when I Flush, does the id get updated >> automatically. Well I watch that ID in a debug watch, and boom... it just >> randomly updates at some point when I'm walking through unrelated fields >> checking for history (not issuing any querys or session activity, as far as >> I can tell). >> >> Just looking for a sanity check if this behavior seems to make sense? I >> would have thought the auto flushing would happen inline with a query... >> like you step over that query or operation, and the autoflush occurs >> synchronously. This is not what I'm seeing. > > -- > 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 post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- 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 post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Expressing a SQL DOMAIN as a class
On Mon, 7 Jan 2019, Mike Bayer wrote: PG's ENUM does work that way, below the same type is shared: from sqlalchemy import Column from sqlalchemy import create_engine from sqlalchemy import Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Session from sqlalchemy.dialects import postgresql Base = declarative_base() State = postgresql.ENUM("AK", "AL", "CO", "NY", name="states") class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) data = Column(State) class B(Base): __tablename__ = 'b' id = Column(Integer, primary_key=True) data = Column(State) Thanks, Mike. Best regards, Rich -- 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 post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Re: Do autoflushes occur asynchronously?
It appears that flush_context.execute() Is the step that determines the FK id of the relationship object and sets that FK id value on the base object. But then it wipes the session's attribute history, as it's been flushed to the DB. Wondering if there's a way to trigger this operation without flushing, so I can interrogate the object and see that that FK id was changed (has a history). On Wednesday, January 16, 2019 at 4:18:36 PM UTC-5, Gmoney wrote: > > I was having a heck of confusing time debugging some code where I was > trying to determine what attrs on an ORM changed prior to committing it. > Every time in my debugger I'd be getting different results (w/r to > History). I finally realized that autoflush was enabled, and now that I > disabled it, things seem much more sane. The strange part though, is that > the update/flush would occur randomly while I was just iterating through > some mapper attributes checking History. No queries, no nothing... but at > one point... whoosh... it must have flushed. > > I'm basing the 'has flushed' inference on the fact that my test update is > updating a foreign key relationship. I update the relationship object (not > the ID) on the primary object. At that point, the FK id is still the old > id from prior to the change. Only when I Flush, does the id get updated > automatically. Well I watch that ID in a debug watch, and boom... it just > randomly updates at some point when I'm walking through unrelated fields > checking for history (not issuing any querys or session activity, as far as > I can tell). > > Just looking for a sanity check if this behavior seems to make sense? I > would have thought the auto flushing would happen inline with a query... > like you step over that query or operation, and the autoflush occurs > synchronously. This is not what I'm seeing. > -- 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 post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Do autoflushes occur asynchronously?
On Wed, Jan 16, 2019 at 4:18 PM Gmoney wrote: > > I was having a heck of confusing time debugging some code where I was trying > to determine what attrs on an ORM changed prior to committing it. Every time > in my debugger I'd be getting different results (w/r to History). I finally > realized that autoflush was enabled, and now that I disabled it, things seem > much more sane. The strange part though, is that the update/flush would > occur randomly while I was just iterating through some mapper attributes > checking History. No queries, no nothing... but at one point... whoosh... it > must have flushed. autoflush only happens when the ORM runs a Query, call commit(), or when you start a SAVEPOINT transaction.You will see autoflush happen when you do things like access attributes, because lazyloading of attributes uses Query in order to load the data. So it is definitely happening "synchronously", that is, there is certainly nothing like a background thread running, but it at first can seem surprising what operations require autoflush to occur. it's entirely reasonable to turn off autoflush if you're only doing simple operations, though often it's a good idea to limit when you disable autoflush only to blocks where necessary using "with session.no_autoflush". I strongly recommend doing a little bit of work in the debugger or console with echo=True turned on so that you can see SQL being emitted. > > I'm basing the 'has flushed' inference on the fact that my test update is > updating a foreign key relationship. I update the relationship object (not > the ID) on the primary object. At that point, the FK id is still the old id > from prior to the change. Only when I Flush, does the id get updated > automatically. Well I watch that ID in a debug watch, and boom... it just > randomly updates at some point when I'm walking through unrelated fields > checking for history (not issuing any querys or session activity, as far as I > can tell). > > Just looking for a sanity check if this behavior seems to make sense? I > would have thought the auto flushing would happen inline with a query... like > you step over that query or operation, and the autoflush occurs > synchronously. This is not what I'm seeing. > > -- > 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 post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- 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 post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Do autoflushes occur asynchronously?
I was having a heck of confusing time debugging some code where I was trying to determine what attrs on an ORM changed prior to committing it. Every time in my debugger I'd be getting different results (w/r to History). I finally realized that autoflush was enabled, and now that I disabled it, things seem much more sane. The strange part though, is that the update/flush would occur randomly while I was just iterating through some mapper attributes checking History. No queries, no nothing... but at one point... whoosh... it must have flushed. I'm basing the 'has flushed' inference on the fact that my test update is updating a foreign key relationship. I update the relationship object (not the ID) on the primary object. At that point, the FK id is still the old id from prior to the change. Only when I Flush, does the id get updated automatically. Well I watch that ID in a debug watch, and boom... it just randomly updates at some point when I'm walking through unrelated fields checking for history (not issuing any querys or session activity, as far as I can tell). Just looking for a sanity check if this behavior seems to make sense? I would have thought the auto flushing would happen inline with a query... like you step over that query or operation, and the autoflush occurs synchronously. This is not what I'm seeing. -- 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 post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.