9

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?

1 Answer 1

8

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")'))
Sign up to request clarification or add additional context in comments.

1 Comment

text('(now() at time zone "utc")')) - does this work with postgres? I dont believe so. Error creating tables: (psycopg2.errors.FeatureNotSupported) cannot use column reference in DEFAULT expression LINE 6: ...ESTAMP WITH TIME ZONE DEFAULT (now() at time zone "utc") NOT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.