Python 对变量表使用SQLAlchemy映射

Python 对变量表使用SQLAlchemy映射,python,sqlalchemy,Python,Sqlalchemy,我有一个数据库,可供所有客户使用。每个客户都有一组表,表后附有其客户ID: 分析学123 嵌入 下载 它是用“手动”SQL语句设置的,但我正在努力将它移植到SQLAlchemy,以便于维护 我更喜欢使用声明性语法,但是有没有什么方法可以动态设置模型的\uu tablename\uu值?如果我需要在一个线程化环境中访问多个客户,会发生什么情况(我们很可能会使用不同的流程,但我宁愿提前说明,以防万一) 除了表名本身,所有关系(简单的单键外键)也必须在表之间正确映射。一种方法是工厂化客户所需的所有

我有一个数据库,可供所有客户使用。每个客户都有一组表,表后附有其客户ID:

  • 分析学123
  • 嵌入
  • 下载
它是用“手动”SQL语句设置的,但我正在努力将它移植到SQLAlchemy,以便于维护

我更喜欢使用声明性语法,但是有没有什么方法可以动态设置模型的
\uu tablename\uu
值?如果我需要在一个线程化环境中访问多个客户,会发生什么情况(我们很可能会使用不同的流程,但我宁愿提前说明,以防万一)


除了表名本身,所有关系(简单的单键外键)也必须在表之间正确映射。

一种方法是工厂化客户所需的所有类。像下面这样的。。。(这只是一个草图!)


不是使用后置表,而是考虑使用单独的数据库或模式。感谢输入-不是100%肯定我会使用这个精确的模型,但类似的东西。
class customer_classes:
    # create the base class
    base = declarative_base(metadata=metadata)

    def __init__(self, metadata, customer_id):

        class analytics(self.base):
            __tablename__ = 'analytics_{}'.format(customer_id)
            __mapper_args__ = ...

        class embeds(self.base):
            __tablename__ = 'analytics_{}'.format(customer_id)
            __mapper_args__ = ...

        class downloads(self.base):
            __tablename__ = 'analytics_{}'.format(customer_id)
            __mapper_args__ = ...

        # now lets assign all these classes to attributes
        import inspect
        defined_classes = [cls for cls in locals().values()
                           if inspect.isclass(cls) and issubclass(cls, base)]
        self.__dict__.update({cls.__name__: cls for cls in defined_classes})

# now we can create a set of classes for customer 123
customer_model = customer_classes(123)
session.query(customer_model. analytics).all()

# you can just as easily work with another customer at the same time in the same session
another_model = customer_classes(456)
session.query(another_model.analytics).all()