I've got a SQLAlchemy table that is defined
foo_table = Table('foo', metadata,
Column('id', Integer, primary_key=True),
Column('created_on', DateTime, default=func.now()),
...
)
which creates a table in Postgres
CREATE TABLE foo
(
id serial NOT NULL,
created_on timestamp without time zone,
...
)
The local timezone is set properly (checked that Python's datetime.datetime.now()
displays my local time), but whenever I insert a row in foo_table
without explicitly setting created_on
, the time used is current UTC instead of my local time (e.g. "2015-07-29 16:38:08.112192").
When I try to set created_on
manually, this all works fine, but it seems that Postgres or SQLAlchemy are converting the time to UTC when leaving it up to func.now()
to assign a timestamp.
How can I get SQLAlchemy or Postgres to just create a record with my current local time?
default=func.now
without the()
or the default value will be the time when the file was imported (e.g. when the server was started in case of a webapp), and not the current time when the row was insertedserver_default=text('now()')
default=func.now()
as is described in the docs: docs.sqlalchemy.org/en/13/core/…func.now() returns the SQL expression object that will render the “NOW” function into the SQL being emitted
I expectfunc.now
wouldn't work.