Setting server_default in sqlalchemy fails

this is what i want to do SQLAlchemy:

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 the default UTC time for the column. In pure psql, as seen above, this succeeds.

This is what the SQLAlchemy code should look like:

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, the problem is with single / double quotes I'm guessing.

I tried

"(now() at time zone \'utc\')"

      

but this results in the same error.

Then I changed 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 get around this?

+3


source to share


1 answer


Consider using the function text

for sqlalchemy:



from sqlalchemy import text
reg_at = db.Column(db.DateTime, server_default=text('(now() at time zone "utc")'))

      

+2


source







All Articles