Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting server_default in sqlalchemy fails

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?

like image 783
kurtgn Avatar asked Jul 24 '15 13:07

kurtgn


1 Answers

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")'))
like image 110
architectonic Avatar answered Sep 28 '22 21:09

architectonic