[英]dynamic table names with SQLalchemy
I am trying to convert old sqlite3 code to sql alchemy. 我正在尝试将旧的sqlite3代码转换为sql alchemy。 I am trying to make sense of how best to handle my use case. 我试图弄清如何最好地处理用例。 I am new to the ORM method of database access. 我是ORM数据库访问方法的新手。
I am trying to dynamically generate unique table names based on a common definition. 我试图根据共同的定义动态生成唯一的表名。 I have read the mixins guide as well as the post on how to use type
to dynamically declare classes , but I am still unsure how of I would go about this. 我已经阅读了mixins指南以及有关如何使用type
来动态声明类的文章 ,但是我仍然不确定如何解决这个问题。 Here is what I have so far: 这是我到目前为止的内容:
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.ext.declarative import declared_attr
Base = declarative_base()
class DynamicName(object):
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
class Genome(DynamicName, Base):
__tablename__ = 'AbstractGenome'
AlignmentId = Column(Integer, primary_key=True)
StartOutOfFrame = Column(Integer)
BadFrame = Column(Integer)
def build_genome_table(genome):
d = {'__tablename__': genome}
table = type(genome, (Genome,), d)
return table
If I try to use this, it doesn't work: 如果我尝试使用此功能,它将无法正常工作:
>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite:///:memory:', echo=True)
>>> genomes = ["A", "B"]
>>> tables = {x: build_genome_table(x) for x in genomes}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <dictcomp>
File "<stdin>", line 3, in build_genome_table
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 55, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
_MapperConfig.setup_mapping(cls, classname, dict_)
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
cfg_cls(cls_, classname, dict_)
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 135, in __init__
self._early_mapping()
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 138, in _early_mapping
self.map()
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 529, in map
**self.mapper_args
File "<string>", line 2, in mapper
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 623, in __init__
self._configure_inheritance()
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 930, in _configure_inheritance
self.local_table)
File "<string>", line 2, in join_condition
File "/cluster/home/ifiddes/python2.7/lib/python2.7/site-packages/sqlalchemy/sql/selectable.py", line 839, in _join_condition
(a.description, b.description, hint))
sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'AbstractGenome' and 'A'.
How do I go about dynamically generating a Genome
table based on a passed name? 如何基于传递的名称动态生成Genome
表? I also would ideally like a setup where I can have hierarchical inheritance, so that I can declare different subclasses like ReferenceGenome
or TargetGenome
which have additional columns but also can have dynamic names. 理想情况下,我还希望有一个可以具有层次继承的设置,以便可以声明不同的子类,例如ReferenceGenome
或TargetGenome
,它们具有附加的列,但也可以具有动态名称。
See: sqlalchemy.orm.mapper
: http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#class-mapping-api . 请参阅: sqlalchemy.orm.mapper
: http : sqlalchemy.orm.mapper
。 My understanding (as I've only modified code using this function in the past) is that it directly maps a model class to a Table object, which itself is connected to a database table. 我的理解(因为我过去仅使用此函数修改过代码)是它直接将模型类映射到Table对象,而Table对象本身已连接到数据库表。
This use-case actually sounds pretty similar to the recipe history_meta: http://docs.sqlalchemy.org/en/latest/_modules/examples/versioned_history/history_meta.html . 这个用例实际上听起来很像食谱history_meta: http : //docs.sqlalchemy.org/en/latest/_modules/examples/versioned_history/history_meta.html 。 It might take some time to sort through, but a Table object is being created here dynamically based on an existing model (any subclass of Versioned), and then directly mapped to the database table when the class is created. 可能要花一些时间来进行排序,但是在此将基于现有模型(Versioned的任何子类)动态创建Table对象,然后在创建类时将其直接映射到数据库表。
Here's the issue though: you do need an actual database table to map to. 但是,这就是问题:您确实需要一个实际的数据库表来映射。 It's an ORM after all. 毕竟这是一个ORM。 You have a few options here: 您可以在此处选择以下几种方式:
If you want to create a table on the fly that will persist in the database, you can use Table.create() as per here: http://docs.sqlalchemy.org/en/latest/core/metadata.html#creating-and-dropping-database-tables 如果要动态创建将保留在数据库中的表,则可以按照以下方式使用Table.create(): http ://docs.sqlalchemy.org/en/latest/core/metadata.html#creating -and惊叹的数据库,表
If you only need to create tables every now and then, you can integrate with alembic: https://pypi.python.org/pypi/alembic 如果您只需要偶尔创建表,则可以与alembic集成: https : //pypi.python.org/pypi/alembic
If you just need it for one process, and never again, you can create temporary tables, though I'm not sure if SQLAlchemy directly supports it. 如果您只需要一个过程,而不再需要它,则可以创建临时表,尽管我不确定SQLAlchemy是否直接支持它。 The few resources I checked seem to be using create() and drop() anyway. 无论如何,我检查的一些资源似乎都在使用create()和drop()。 I haven't used SQLAlchemy 1.0+, so it may have some support somewhere that I haven't seen. 我还没有使用SQLAlchemy 1.0+,所以它可能在我未曾见过的地方获得了一些支持。
Let me know if anything here isn't clear. 让我知道这里是否不清楚。 It's been a while since I've played with history_meta.py, so I may be rusty. 自从我玩过history_meta.py已经有一段时间了,所以我可能会生锈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.