deferred column_properties may be less-efficient subquery selects (and thus
marked deferred). When a flush occurs that updates an object, any
read-only column_properties are marked as expired, even if they weren't
even loaded. This means if the object needs to be refreshed, all these
deferred column properties are loaded.
We probably want the behavior to only expire read-only attributes that were
actually loaded, right?
See attached script. This behavior is as of 1.1.1
Thoughts?
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
from sqlalchemy import *
from sqlalchemy.orm import *
engine = create_engine('postgresql://URL',echo=True)
maker = sessionmaker(autoflush=True, autocommit=False)
DBSession = scoped_session(maker)
metadata = MetaData(engine)
table=Table("sometable", metadata,
Column("id_a", Unicode(255), primary_key=True),
Column("id_b", Unicode(255)),
)
class SomeClass(object):
pass
mapper(SomeClass, table,
properties = {
'inefficient_subselect':
# suppose this deferred column property isn't very efficient
column_property(
(table.c.id_a + table.c.id_b).label('inefficient_subselect'),
deferred=True)
}
)
metadata.create_all()
try:
session = DBSession()
obj = SomeClass()
obj.id_a = 'PK1'
session.add(obj)
session.flush()
session.commit()
DBSession.remove()
session = DBSession()
session.begin_nested()
o = session.query(SomeClass).get('PK1')
o.id_b = '01'
session.flush()
if 'inefficient_subselect' in o._sa_instance_state.expired_attributes:
raise Exception("inefficient_subselect wasn't loaded before (and is "
"deferred), so probably shouldn't be in expired_attributes")
session.rollback()
# if "raise" is removed, this loads the deferred column_property:
o.id_a
finally:
DBSession.remove()
metadata.drop_all()