繁体   English   中英

如何使用sqlalchemy为现有表创建临时表?

[英]How to create a temporary table for an existing table with sqlalchemy?

我有一个与以下类关联的表(在mysql服务器中):

class Number(Base):
    __table_name__ = 'number'
    col0 = Column(...) # PRIMARY KEY
    col1 = Column(...)
    col2 = Column(...)
    col3 = Column(...)

我想在程序启动时为此编号表创建一个临时表,该临时表如下所示:

class tempNumber(Base):
      __table_name__ = 'temp_number'
      col0 = Column(...) # direct from Number.col0
      col1 = Column(...) # from Number.col1, but will be modified, for example tempNumber.col1 = Number.col1.replace(' ','')

我还尝试将以下行添加到类Number中:

__table_args__ = {'prefixes': ['TEMPORARY']}

但是我的python程序告诉我表temp_number不存在。

还有一个问题是,如何快速复制数据? 因为表号中有很多行。

  1. temp_number表实际上存在于基础数据库中吗?

  2. 通常,无法通过sqlalchemy完成将数据从一个表快速复制到另一个表的工作,而sqlalchemy在此类任务中会产生大量开销。 相反,它会根据您的基础数据库而有所不同。

例如, 这是在MySQL中如何做到的 这是使用PostgreSQL的一种方法。 但是,这通常不会复制索引之类的内容。 这样,您将必须通过数据库驱动程序手动进行设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM