SQLAlchemy KeyError在日期时间列

时间:2019-02-04 19:07:43

标签: python mysql sqlalchemy

我有一个mysql数据库,该数据库应使用sqlalchemy存储加密价格数据。尽管sqlalchemy记录器为我提供了“ Timestamp”字段的KeyError,但添加和更新对象仍然有效。从日志中我并没有多大意义,所以我希望有人可以指出错误的方向。

我尝试插入带有时间戳数据的日期时间对象以及字符串。

致电

        self.dbcon = DatabaseController()
        self.bitmex_client = BitmexClient(self.parse_message, topics, True)
        # self.trade_controller = TradeController(self.bitmex_client, self.dbcon)

    def parse_message(self, msg):
        # print(msg)
        if 'table' in msg:
            if msg['table'] == 'tradeBin1m':
                # todo run analysis
                if msg['data'][0]:
                    m = msg['data'][0]
                    ts = datetime.strptime(m['timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ')
                    candle = Candle(ts, m['symbol'], m['open'], m['close'], m['high'], m['low'], m['volume'])
                    self.dbcon.store(candle)

主要内容

from sqlalchemy import *
from enums import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()
engine = create_engine("mysql+pymysql://un:pw@localhost/db", echo="debug")
Session = sessionmaker(bind=engine)


class DatabaseController:

    def __init__(self):
        self.sesh = Session()

    def store(self, item):
        self.sesh.add(item)
        self.sesh.commit()

    def update(self, item):
        if type(item) is Order:
            self.sesh.query(Order).filter_by(order_id=item.order_id).update(
                {col: getattr(item, col) for col in Order.__table__.columns.keys()})
            self.sesh.commit()

    def delete(self, item):
        if type(item) is Order:
            self.sesh.query(Order).filter_by(order_id=item.order_id).delete()
        self.sesh.commit()


class Candle(Base):

    __tablename__ = 'Candles'

    timestamp = Column(DateTime(), primary_key=True)
    symbol = Column(String(20), primary_key=True)
    open = Column(FLOAT)
    close = Column(FLOAT)
    high = Column(FLOAT)
    low = Column(FLOAT)
    volume = Column(FLOAT)

    def __init__(self, ts, symbol, price_open, price_close, high, low, volume):
        self.timestamp = ts
        self.symbol = symbol
        self.open = price_open
        self.close = price_close
        self.high = high
        self.low = low
        self.volume = volume

日志

2019-02-04 18:12:51,311 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2019-02-04 18:12:51+0100 [-] --- Logging error ---
2019-02-04 18:12:51+0100 [-] Traceback (most recent call last):
2019-02-04 18:12:51+0100 [-]   File "C:\Users\busabuzz\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 983, in emit
2019-02-04 18:12:51+0100 [-]     msg = self.format(record)
2019-02-04 18:12:51+0100 [-]   File "C:\Users\busabuzz\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 829, in format
2019-02-04 18:12:51+0100 [-]     return fmt.format(record)
2019-02-04 18:12:51+0100 [-]   File "C:\Users\busabuzz\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 572, in format
2019-02-04 18:12:51+0100 [-]     s = self.formatMessage(record)
2019-02-04 18:12:51+0100 [-]   File "C:\Users\busabuzz\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 541, in formatMessage
2019-02-04 18:12:51+0100 [-]     return self._style.format(record)
2019-02-04 18:12:51+0100 [-]   File "C:\Users\busabuzz\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py", line 384, in format
2019-02-04 18:12:51+0100 [-]     return self._fmt % record.__dict__
2019-02-04 18:12:51+0100 [-] KeyError: 'timestamp'

0 个答案:

没有答案