Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不同的数据库模式中重用sqlalchemy声明性模型_Python_Database_Sqlalchemy - Fatal编程技术网

Python 如何在不同的数据库模式中重用sqlalchemy声明性模型

Python 如何在不同的数据库模式中重用sqlalchemy声明性模型,python,database,sqlalchemy,Python,Database,Sqlalchemy,我正在为多个数据库模式实现sqlalchemy声明性模型集。这些数据库模式在表设计上非常相似。我希望能够在每个模式的模型集合中尽可能多地重用公共模型代码(列定义、关系等)。 (请注意,所讨论的模式99%相同。) 举个小例子: from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Integer, String Base = declarative_base() class Person(B

我正在为多个数据库模式实现sqlalchemy声明性模型集。这些数据库模式在表设计上非常相似。我希望能够在每个模式的模型集合中尽可能多地重用公共模型代码(列定义、关系等)。 (请注意,所讨论的模式99%相同。)

举个小例子:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Integer, String

Base = declarative_base()

class Person(Base):
    __tablename__ = 'person'
    name = Column(String, primary_key=True)
    age = Column(Integer)
如果我现在想引入一个新的属性Person列“height”,它只存在于一个模式中,而不存在于另一个模式中,那么在重用现有Person列定义的同时,如何定义它呢

到目前为止,我已经尝试使用声明性mixin来定义公共功能,然后将它们混合到继承自不同模式特定基础的类中。然而,这并不令人满意,因为即使是大多数相同的模型也必须声明为mixin,并且一个声明性类必须为schema声明一次(从适当的基和mixin继承)。鉴于模式相似,这看起来像是很多样板代码

有更好的方法吗