我通过declarative_base创建了一个模型:
Base = declarative_base()
class Record(Base):
__tablename__ = 'test'
id = Column(BigInteger, primary_key=True)
datefrom = Column(Date)
dateto = Column(Date)
active = Column(Boolean, index=True, default=True)
created = Column(DateTime, default=datetime.datetime.utcnow)
当我尝试将模块保存到postgres数据库时:
dfrom = datetime.datetime.strptime(somestring, '%d/%m/%Y').date()
dto = datetime.datetime.strptime(someotherstring, '%d/%m/%Y').date()
record = Record(datefrom=dfrom, dateto=dto)
session.add(record)
session.commit()
我收到了以下错误:
(DataError) invalid input syntax for type date: \"\"\nLINE 1: ...'2013-11-26'::date, '2013-12-27'::date...\n ^\n 'INSERT INTO test (datefrom, dateto, active, created) VALUES (%(datefrom)s, %(dateto)s, %(active)s, %(created)s) RETURNING test.id' {'created': datetime.datetime(2013, 11, 27, 20, 12, 55, 716185), 'dateto': datetime.date(2013, 12, 27), 'datefrom': datetime.date(2013, 11, 26), 'active': True}
类似的错误是针对sqlite后端:
SQLite Date type only accepts Python date objects as input. (original cause: TypeError: SQLite Date type only accepts Python date objects as input.) 'INSERT INTO test (datefrom, dateto, active, created) VALUES (?, ?, ?, ?)' [{'dateto': datetime.date(2013, 12, 27), 'datefrom': datetime.date(2013, 11, 26)}]
那么我做错了什么?不应该datetime.date对象那样工作吗?
答案 0 :(得分:2)
不是回答*,只是想进一步调查 :
无法在SQLite上重现问题。下面的自包含代码运行没有问题,并产生提供的输出。你在做什么不同?你能否解决一个类似的例子,看看它有什么不同? (注:使用SA v0.8.3)
<强> CODE:强>
import datetime
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
engine = create_engine('sqlite:///:memory:', echo=True)
Session = sessionmaker(bind=engine)
# object model
class Record(Base):
__tablename__ = 'test'
id = Column(Integer, primary_key=True)
datefrom = Column(Date)
dateto = Column(Date)
active = Column(Boolean, index=True, default=True)
created = Column(DateTime, default=datetime.datetime.utcnow)
# create db schema
Base.metadata.create_all(engine)
session = Session()
# TEST INSERT DATA
somestring, someotherstring = '3/1/2008', '1/3/1988'
dfrom = datetime.datetime.strptime(somestring, '%d/%m/%Y').date()
dto = datetime.datetime.strptime(someotherstring, '%d/%m/%Y').date()
record = Record(datefrom=dfrom, dateto=dto)
session.add(record)
session.commit()
session.expunge_all()
<强>输出:强>
2013-11-28 09:17:14,608 INFO sqlalchemy.engine.base.Engine PRAGMA table_info("test")
2013-11-28 09:17:14,608 INFO sqlalchemy.engine.base.Engine ()
2013-11-28 09:17:14,609 INFO sqlalchemy.engine.base.Engine
CREATE TABLE test (
id INTEGER NOT NULL,
datefrom DATE,
dateto DATE,
active BOOLEAN,
created DATETIME,
PRIMARY KEY (id),
CHECK (active IN (0, 1))
)
2013-11-28 09:17:14,611 INFO sqlalchemy.engine.base.Engine ()
2013-11-28 09:17:14,611 INFO sqlalchemy.engine.base.Engine COMMIT
2013-11-28 09:17:14,611 INFO sqlalchemy.engine.base.Engine CREATE INDEX ix_test_active ON test (active)
2013-11-28 09:17:14,611 INFO sqlalchemy.engine.base.Engine ()
2013-11-28 09:17:14,612 INFO sqlalchemy.engine.base.Engine COMMIT
2013-11-28 09:17:14,618 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2013-11-28 09:17:14,618 INFO sqlalchemy.engine.base.Engine INSERT INTO test (datefrom, dateto, active, created) VALUES (?, ?, ?, ?)
2013-11-28 09:17:14,618 INFO sqlalchemy.engine.base.Engine ('2008-01-03', '1988-03-01', 1, '2013-11-28 08:17:14.619000')
2013-11-28 09:17:14,619 INFO sqlalchemy.engine.base.Engine COMMIT