尝试使用DateTime时出现SQLalchemy截断错误?

时间:2019-03-15 16:24:23

标签: python mysql datetime sqlalchemy

这是我的桌子

from sqlalchemy import Column, Integer, String, Time, DateTime, Float
from sqlalchemy.orm import relationship
from database.base import Base


class Portfolio(Base):

__tablename__ = 'portfolio'

company_id = Column(Integer, primary_key=True)
ticker_id = Column(String(10))
company_name = Column(String(40))
natal = Column(DateTime)
longitude = Column(Float)
latitude = Column(Float)

history = relationship('History', backref='portfolio')

def __init__(self, company_id, ticker_id, company_name, longitude, latitude, natal):
    self.company_id = company_id
    self.ticker_id = ticker_id
    self.company_name = company_name
    self.longitude = longitude
    self.latitude = latitude
    self.natal = natal

def __repr__(self):
    return "<Portfolio(company_id='%s', ticker_id='%s', company_name='%s', longitude='%s', latitude='%s', natal='%s')>" % \
           (self.company_id, self.ticker_id, self.company_name, self.longitude, self.latitude, self.natal)

当我运行以下命令时:

import datetime
session = Session()
apple = Portfolio(1, 'AAPL', 'Apple', datetime.datetime(1980, 12, 12, 10, 0 , 0), -122.0322895, 37.3228934)
session.add(apple)
session.flush()

我收到以下错误消息:

/Users/akasha/PycharmProjects/Akasha/venv/lib/python3.7/site- packages/pymysql/cursors.py:170: Warning: (1265, "Data truncated for column 'natal' at row 1")
result = self._query(query)
/Users/akasha/PycharmProjects/Akasha/venv/lib/python3.7/site- packages/pymysql/cursors.py:170: Warning: (1265, "Data truncated for column 'longitude' at row 1")
result = self._query(query)

不确定声明表时是否需要为natal列指定更多信息?

任何帮助将不胜感激,否则我现在将使用字符串存储日期。

0 个答案:

没有答案