我的程序中需要一个临时表。我已经看到这可以通过“映射器”语法以这种方式实现:
t = Table(
't', metadata,
Column('id', Integer, primary_key=True),
# ...
prefixes=['TEMPORARY'],
)
看到 here
但是,我的整个代码都在使用声明性基础,这是我的理解,我想坚持下去。有可能使用 hybrid approach但如果可能的话,我会避免它。
这是我的声明类的简化版本:
import SQLAlchemy as alc
class Tempo(Base):
"""
Class for temporary table used to process data coming from xlsx
@param Base Declarative Base
"""
# TODO: make it completely temporary
__tablename__ = 'tempo'
drw = alc.Column(alc.String)
date = alc.Column(alc.Date)
check_number = alc.Column(alc.Integer)
提前致谢!
编辑新问题:
现在类看起来像这样:
import SQLAlchemy as alc
class Tempo(Base):
"""
Class for temporary table used to process data coming from xlsx
@param Base Declarative Base
"""
# TODO: make it completely temporary
__tablename__ = 'tempo'
__table_args__ = {'prefixes': ['TEMPORARY']}
drw = alc.Column(alc.String)
date = alc.Column(alc.Date)
check_number = alc.Column(alc.Integer)
当我尝试向该表中插入数据时,出现以下错误消息:
sqlalchemy.exc.OperationalError: (OperationalError) no such table:
tempo u'INSERT INTO tempo (...) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' (....)
表似乎不存在只是通过声明它。我已经看到像 create_all() 这样的东西可能是解决这个问题的方法(很有趣的是在彻底解释的同时看到新的想法是如何产生的)
那么,再次感谢您!
最佳答案
是否可以使用__table_args__
?参见 https://docs.sqlalchemy.org/en/14/orm/declarative_tables.html#orm-declarative-table-configuration
class Tempo(Base):
"""
Class for temporary table used to process data coming from xlsx
@param Base Declarative Base
"""
# TODO: make it completely temporary
__tablename__ = 'tempo'
__table_args__ = {'prefixes': ['TEMPORARY']}
drw = alc.Column(alc.String)
date = alc.Column(alc.Date)
check_number = alc.Column(alc.Integer)
关于python - 具有声明性基础的 SQLAlchemy 临时表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13455207/