On 05/20/2016 05:16 AM, Михаил Доронин wrote:
The first link you gave have exactly the thing I said I don't want to do.
The second one is however looks very promising.


oh well when you said "map(klass, *tables)", that's pretty much exactly what entity name does. But I guess you don't like having to qualify each Query with the subclass you want to deal with.


Have I understand correctly that I need to create custom Session class
and custom Query class for this session that would somehow produce
correct from clause (with correct table name) from a statement like that

|
session.query(ShardedEntity).filter(ShardedEntity.some_unique_name=='foo')
|

If so, than could you give a little more guidance on it? Like what
methods from query do I need do override etc?

to make this "automatic" is very tricky. it means that the SQL the query is going to produce has to be intercepted at some point and rewritten. We have a long term plan to add a new API to Query to make this kind of thing feasible at the point at which the Query is to begin producing a core select() object, that API is only a single event so far and is described here: http://docs.sqlalchemy.org/en/latest/orm/events.html#sqlalchemy.orm.events.QueryEvents . Altering the Query to hit a different table for a certain entity is not that easy, though.

There's another recipe i just remembered for this kind of thing which hits the SELECT statement way at the end after it's already a string. You replace the table name out based on the fact that the original Table() object has some very easy-to-identity name. This might be just what you're asking for and that recipe is here: https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/SessionModifiedSQL.



On Thursday, 19 May 2016 18:30:51 UTC+3, Mike Bayer wrote:



    Well if you want to see some examples of on-the-fly mappers for
    different databases that nonetheless map to the same class take a look
    at
    https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/EntityName
    <https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/EntityName>.
        The kind of thing you're doing has been done before but it's not
    totally easy.


    Along with the entity name thing, you can customize Session as well as
    how it generates a query.  If you look at the source to the horizontal
    sharding extension
    
http://docs.sqlalchemy.org/en/latest/orm/extensions/horizontal_shard.html?highlight=horizontal#module-sqlalchemy.ext.horizontal_shard
    
<http://docs.sqlalchemy.org/en/latest/orm/extensions/horizontal_shard.html?highlight=horizontal#module-sqlalchemy.ext.horizontal_shard>

    and maybe also my post at
    
http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/
    
<http://www.google.com/url?q=http%3A%2F%2Ftechspot.zzzeek.org%2F2012%2F01%2F11%2Fdjango-style-database-routers-in-sqlalchemy%2F&sa=D&sntz=1&usg=AFQjCNHCTZJLLutlOvl1P_S-Ua1Fn4MwHg>

    you can get some ideas for how to game things on the Session side.

    Those are the two tools you'd be working with here so hopefully you can
    find a way to combine them in a way that works for you.




    >
    > I haven't created the issue because I didn't know if solution (not a
    > workaround like mentioned in stackoverflow) already possible with
    > sqlalchemy. If not it would be beneficial to add I think.
    >
    > --
    > 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] <javascript:>
    > <mailto:[email protected] <javascript:>>.
    > To post to this group, send email to [email protected]
    <javascript:>
    > <mailto:[email protected] <javascript:>>.
    > Visit this group at https://groups.google.com/group/sqlalchemy
    <https://groups.google.com/group/sqlalchemy>.
    > For more options, visit https://groups.google.com/d/optout
    <https://groups.google.com/d/optout>.

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

--
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.

Reply via email to