On 11/24/2016 11:04 PM, Srikanth Bemineni wrote:
Hi,
I am using sqlalchemy in my pyramid application. I have situation here,
where I need to map a table on the fly in the application during run
time. I also dont want to re-map all tables, there are some table which
are defined using a declarative base. I need those mapping to remain as
is. How can we do this?. Does clear_mappers() remove even declarative
base mapping ?
clear_mappers() is not for application use, only for test suites, since
it wipes all mappers.
You can make ad-hoc mappings that will be garbage collected, *if* you
don't point other non-GC'ed mappings to them using relationships or
backrefs, by making a subclass, mapping that, then dropping the
subclass. An example of this pattern is at:
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/EntityName .
However, I don't recommend this pattern, if you are actually making
thousands of different mappings and dropping them - it won't scale.
mappings are expensive to create and garbage collect, and it is easy to
accidentally build a backref that will cause it to not be garbage collected.
The more "high scale" way here if you're really doing hundreds/thousands
of different sets of tables is to modify the SQL statements on the way
out. That example is here:
https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/SessionModifiedSQL
. This does exactly the use case you describe, which is that
per-request, a subset of tables need to have specific names.
SQLAlchemy now includes a feature which does what this recipe does, but
only for the "schema" name of the Table:
https://docs.sqlalchemy.org/en/latest/core/connections.html?highlight=schema%20execution%20option#schema-translating
. For more generalized modification of table names on the fly, the wiki
recipe is more useful.
DBSession =
scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
class User(Base):
__tablename__ ="youzer"
uid = Column(Integer,autoincrement=True, primary_key=True)
uname = Column(Text,nullable=False,index=True)
Just one of the tables as example. There are many tables that I need to
map for different request
class GroupOrder()
gtid = Column(Integer, autoincrement=True,primary_key=True)
group_id = Column(Integer,
ForeignKey("group.gid",ondelete="CASCADE"),nullable=False)
...
...
These groups can grow huge , so decision was made to shard these group
based on a hash. These are similar structured tables , with different names.
Tables
group_order_<shard1>
group_order_<shard1>
group_order_<shard1>
table_object = Table(<shard table name with similar structure>,
Base.metadata,
Column('gtid', Integer, primary_key=True),
Column('group_id', ForeignKey('group.gid',ondelete="CASCADE"),
nullable=False),
....
...
Based on the request, I need to map the GroupOrder to the concerned
table and process the request.
This is a uwsgi application will the clear_mapper affect other worker
threads. ?
Srikanth Bemineni
--
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.