We are using sqlalchemy and postgres extensively, and have come across a 
problem with deferred columns when an object is merged into a session with 
load=False. Under these conditions, we find that the deferred columns are 
not deferred - i.e. they loaded when any attribute is accessed, rather than 
being deferred until explicitly accessed. We are using sqlalchemy 0.9.7 and 
postgres 9.4.1.

Here's a simple example that illustrates the problem using the following 
table:

class Port(Base):
    ''' Table port in schema comm. '''
    __tablename__ = "port"
    name          = Column(String, primary_key=True)
    port          = deferred(Column(Integer, unique=True))


First we try querying Port on name:

my_port = session.query(Port).filter_by(name='test_thing').one()
my_port.name

This code produces the following query, showing that the port column is 
correctly deferred:

2015-07-16 15:44:08.539 : sqlalchemy.engine.base.Engine : 
base.py:912(_execute_context) : INFO : SELECT comm.port.name AS 
comm_port_name

FROM comm.port

WHERE comm.port.name = %(name_1)s


Now I expire and expunge the my_port object, and then merge it back into 
the session with load=False, and access name like this:

session.expire(my_port)
session.expunge(my_port)
new_port = session.merge(my_port, load=False)
new_port.name

Which produces the following query, showing that the port column has not 
been deferred, and has instead been included in the query that fetches name:

2015-07-16 15:44:08.539 : sqlalchemy.engine.base.Engine : 
base.py:912(_execute_context) : INFO : SELECT comm.port.port AS 
comm_port_port, comm.port.name AS comm_port_name 

FROM comm.port 

WHERE comm.port.name = %(param_1)s


If I do not set load=False, the column is correctly deferred. I've also 
tried creating a deferred object directly rather than expunging one from 
the session and the problem still occurs, so it seems to be caused by the 
load=False option.


Thanks,

Tom

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to