On 12/06/2016 05:43 PM, Clare Curtis wrote:
Hey,

I have a couple questions about querying with tables that have a
'deleted' column for soft deletion.

1. Both PreFilteredQuery
<https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PreFilteredQuery> and
QueryEvents
<http://docs.sqlalchemy.org/en/latest/orm/events.html?highlight=query%20event#query-events>
 have
examples related to automatically adding a filter to remove these hidden
rows from the returned results, but neither of them really addresses how
you could override that filter to include the hidden rows. Is it
possible to do this with either or both of these? We have a use case
where 99% of the time we don't want to see these hidden rows, but also
have to support the 1% of the time when they're needed.

well those are recipes for custom subclasses of Query. you could add additional methods that enable/disable the filtering criteria.

There's also the more common approach of just architechting your application to use a factory method for queries. that is, instead of saying:

    q = session.query(thing).filter(...)

you'd say

    q = produce_query(session, thing, soft_deleted=False).filter(...)

methods on a Query subclass, or custom factory function, just a matter of which works out better for you....



2. The PreFilteredQuery
<https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PreFilteredQuery> 
example
is also filtering any hidden related objects (such as when address.user
is None). Which part of the LimitingQuery class is causing that
relationship filtering to happen?

in that example the LimitingQuery is plugged into the Session, so when a lazy load for related objects occurs, it consults the Session to load the objects, which results in using LimitingQuery to create the lazy load.


Thanks!
Clare

--
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
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto: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.

Reply via email to