maslak
maslak

Reputation: 1265

How to insert a value into SQLAlchemy TIMESTAMP column

I am trying to set value for timestamp column using SQLAlchemy, but I experience following error:

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

My table looks as folows:

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

Insertion try looks like this:

SomeTable(timestamp=time.time())

When I use datetime.now() instead of time.time() the record is inserted into the table but when I select it from the database it has following format:

2019-04-02 11:44:24.801046

So it looks like TIMESTAMP field does not store timestamp format.

Is it regular postgres behaviour? Or am I missing something?

Upvotes: 5

Views: 15335

Answers (1)

Milenko Jevremovic
Milenko Jevremovic

Reputation: 1056

I think that is good, because next is correct:

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

so by default you have datetime.now() there, it is about presentation

datetime.now() will give you '2019-04-02 14:21:33.715782

datetime.now().isoformat() will give you '2019-04-02T14:31:09.071147'

Check it with: http://www.sqlfiddle.com/#!15/d0c6a/8

If you uncomment third insert you will get exact same exception as yours

Upvotes: 4

Related Questions