如何将值插入SQLAlchemy TIMESTAMP列

问题描述 投票:1回答:1

我正在尝试使用SQLAlchemy为timestamp列设置值,但我遇到以下错误:

column "timestamp" is of type timestamp without time zone but expression is of type numeric

我的表格如下:

class SomeTable(db.Model):
    timestamp = Column(TIMESTAMP)

插入尝试看起来像这样:

SomeTable(timestamp=time.time())

当我使用datetime.now()而不是time.time()时,记录被插入到表中,但是当我从数据库中选择它时,它具有以下格式:

2019-04-02 11:44:24.801046

所以看起来TIMESTAMP字段不存储时间戳格式。

这是常规的postgres行为吗?或者我错过了什么?

python python-3.x postgresql sqlalchemy timestamp
1个回答
1
投票

我认为这很好,因为接下来是正确的:

Column('timestamp', TIMESTAMP(timezone=False), nullable=False, default=datetime.now())

所以默认你有datetime.now()那里,它是关于演示

datetime.now()会给你'2019-04-02 14:21:33.715782

datetime.now().isoformat()会给你'2019-04-02T14:31:09.071147'

检查:http://www.sqlfiddle.com/#!15/d0c6a/8

如果取消注释第三个插入,您将获得与您的完全相同的例外

© www.soinside.com 2019 - 2024. All rights reserved.