On 05/19/2016 08:58 AM, Михаил Доронин wrote:
Suppose I have models (with corresponding tables) in pseudocode

table1
| some_unique_name | some_property |
|------------------+---------------|
| foo              | 1             |
|------------------+---------------|
| bar              | 2             |
|------------------+---------------|

table2
| another_unique_name | another_property |
|---------------------+------------------|
| jack                | married          |
|---------------------+------------------|
| marry               | widow            |
|---------------------+------------------|


Another database

foo_jack_some_huge_data
| kids_count | dog_coung | point_in_time |
|------------+-----------+---------------|
| 1          | 0         | 0:01          |
|------------+-----------+---------------|
| 2          | 1         | 3:00          |
|------------+-----------+---------------|

bar_jack_some_huge_data
| kids_count | dog_coung | point_in_time |
|------------+-----------+---------------|
| 2          | 0         | 0:01          |
|------------+-----------+---------------|
| 3          | 1         | 3:00          |
|------------+-----------+---------------|


foo_marry_some_huge_data
| kids_count | dog_coung | point_in_time |
|------------+-----------+---------------|
| 4          | 0         | 0:01          |
|------------+-----------+---------------|
| 5          | 1         | 3:00          |
|------------+-----------+---------------|


So what I'm trying to say that we are making a shard on
table1.some_unique_name and table2.another_unique_name.

So what I've already done is that


|
|
|
@lru_cache(maxsize=None)
def candles_table(some_unique_name, another_unique_name):

return Table('_'.join((some_unquie_name, another_unique_name, 
'some_huge_data')), Base.metadata,
         Column('kids_count', Integer),
         Column('dog_count', Integer),
         Column('point_in_time', DateTime),
    )
|
|

|




this works fine but I'd like to use class and a mapper instead of raw
table object (for obvious reasons)
I saw this question
<http://stackoverflow.com/questions/19163911/dynamically-setting-tablename-for-sharding-in-sqlalchemy>,
but I don't like the solutions there.
Can I create a custom mapper class that would have api like that

|
mapper.map(klass, *tables)
|

and then somehow when quering this class via session.query I would be
able to say what tables exactly would I like to query. Like some
.shard(...) or something like that.

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. 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 and maybe also my post at http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/ 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 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.

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