inspect a scalar relationship property when it is loaded

21 views
Skip to first unread message

Kent

unread,
Oct 28, 2016, 10:48:48 AM10/28/16
to sqlalchemy
@validates and 'set' attribute events will only fire when the user sets a property (setattr), not when initially loaded by the orm.

Is there a way to intercept (for inspection) a scalar relationship property instance when it is loaded?  I don't think the 'load' event will work because I won't know if the current instance is being loaded in the context of setting an instrumented attribute.

For example:

If I have a parent object, I want to know when parent.childobj is populated by the orm framework (whether on a lazy load or joined load).

Is that possible?

Thanks in advance!

mike bayer

unread,
Oct 28, 2016, 11:21:57 AM10/28/16
to sqlal...@googlegroups.com


On 10/28/2016 10:48 AM, Kent wrote:
> @validates and 'set' attribute events will only fire when the /user/
> sets a property (setattr), not when initially loaded by the orm.
>
> Is there a way to intercept (for inspection) a scalar relationship
> property instance when it is loaded? I don't think the 'load' event
> will work because I won't know if the current instance is being loaded
> in the context of setting an instrumented attribute.
>
> For example:
>
> If I have a parent object, I want to know when parent.childobj is
> populated by the orm framework (whether on a lazy load or joined load).
>
> Is that possible?
>
> Thanks in advance!

based on the laws of twos, being that any random issue that has never
been asked ever for ten years will suddenly be asked for twice within
two days, the answer is no! the event proposed would be
collection_populate, at the same time we'd add scalar_populate, see
https://groups.google.com/d/msg/sqlalchemy/IfuUwUgKwW0/5ZgPmpAvBAAJ .

Interim solution is to subclass Query and override __iter__. The
load() event will get you eagerly-loaded attributes though, set it on
"parent"'s class and then inspect "childobj".




>
> --
> 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
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Kent Bower

unread,
Oct 28, 2016, 11:55:42 AM10/28/16
to sqlal...@googlegroups.com
The load() event would also capture if it were subqueryload()ed, right?

And overriding Query.__iter__ wouldn't catch the cases where the scalar is a "use_get" lookup that was already in session.identity_map...  No good way to intercept attributes.set_committed() for that, is there?


To post to this group, send email to sqlal...@googlegroups.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 a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/5cB4T37sWOk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+unsubscribe@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.

mike bayer

unread,
Oct 28, 2016, 1:13:02 PM10/28/16
to sqlal...@googlegroups.com


On 10/28/2016 11:55 AM, Kent Bower wrote:
> The load() event would also capture if it were subqueryload()ed, right?

any eagerload.


>
> And overriding Query.__iter__ wouldn't catch the cases where the scalar
> is a "use_get" lookup that was already in session.identity_map...

right, would need to catch that too.

No
> good way to intercept attributes.set_committed() for that, is there?

there's not events for that because setting attribute values for loads
is usually done using more direct paths than that and speed is extremely
critical. that's why there's not a lot of "load" events. new events
that are local to relationship loading are feasible though.
> an email to sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>
> <mailto:sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>
> <mailto:sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>>.
> <https://groups.google.com/group/sqlalchemy>.
> For more options, visit https://groups.google.com/d/optout
> <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
> <http://stackoverflow.com/help/mcve> for a full description.
> --- You received this message because you are subscribed to a topic
> in the Google Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sqlalchemy/5cB4T37sWOk/unsubscribe
> <https://groups.google.com/d/topic/sqlalchemy/5cB4T37sWOk/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email to
> sqlalchemy+...@googlegroups.com
> <mailto:sqlalchemy%2Bunsu...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> <https://groups.google.com/group/sqlalchemy>.
> For more options, visit https://groups.google.com/d/optout
> <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+...@googlegroups.com
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages