I am trying to convert old sqlite3 code to 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.
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 var url = hyperL.getFormulaR1C1();
to dynamically declare classes, but I am still unsure how of I would go about this. Here is what I have so far:
type
If I try to use this, it doesn't work:
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
How do I go about dynamically generating a >>> 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'.
table based on a passed name? I also would ideally like a setup where I can have hierarchical inheritance, so that I can declare different subclasses like Genome
or ReferenceGenome
which have additional columns but also can have dynamic names.
答案 0 :(得分:1)
请参阅:sqlalchemy.orm.mapper
:http://docs.sqlalchemy.org/en/latest/orm/mapping_api.html#class-mapping-api。我的理解(因为我过去只使用此函数修改过代码)是它直接将模型类映射到Table对象,该对象本身连接到数据库表。
这个用例听起来非常类似于食谱history_meta:http://docs.sqlalchemy.org/en/latest/_modules/examples/versioned_history/history_meta.html。排序可能需要一些时间,但是这里基于现有模型(Versioned的任何子类)动态创建Table对象,然后在创建类时直接映射到数据库表。
这是问题所在:您确实需要一个实际的数据库表来映射。毕竟这是一个ORM。你有几个选择:
如果要动态创建将在数据库中保留的表,可以按照此处使用Table.create():http://docs.sqlalchemy.org/en/latest/core/metadata.html#creating-and-dropping-database-tables
如果您只需要偶尔创建表格,则可以与alembic集成:https://pypi.python.org/pypi/alembic
如果您只需要一个进程,而且再也不需要它,您可以创建临时表,但我不确定SQLAlchemy是否直接支持它。我检查的少数资源似乎正在使用create()和drop()。我没有使用过SQLAlchemy 1.0+,所以它可能会在某些我没见过的地方得到支持。
如果有什么不清楚,请告诉我。我玩了history_meta.py已经有一段时间了,所以我可能会生锈。