this is the stuff that I want SQLAlchemy to do:
blesk_dev=# alter table address add column reg_at timestamp without time zone default (now() at time zone 'utc');
ALTER TABLE
that is, I want to set a default UTC time for a column. In pure psql, as seen above, this succeeds.
This is what it's supposed to look like in SQLAlchemy code:
reg_at = db.Column(db.DateTime, server_default="(now() at time zone 'utc')")
But when I try to migrate I get this:
sqlalchemy.exc.ProgrammingError: (ProgrammingError) syntax error at or near "utc"
LINE 1: ...STAMP WITHOUT TIME ZONE DEFAULT '(now() at time zone 'utc')'
^
"ALTER TABLE address ADD COLUMN reg_at TIMESTAMP WITHOUT TIME ZONE DEFAULT '(now() at time zone 'utc')'" {}
So there's an issue with single/double quotes I presume.
I tried
"(now() at time zone \'utc\')"
but this gets me the same error.
I then swapped the quotes:
'(now() at time zone "utc")'
'(now() at time zone \"utc\")'
to no avail:
sqlalchemy.exc.DataError: (DataError) invalid input syntax for type timestamp: "(now() at time zone "utc")"
'ALTER TABLE address ADD COLUMN reg_at TIMESTAMP WITHOUT TIME ZONE DEFAULT \'(now() at time zone "utc")\''
How do I work around this?
Consider using the text
function of sqlalchemy:
from sqlalchemy import text
reg_at = db.Column(db.DateTime, server_default=text('(now() at time zone "utc")'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With