I have an SQLAlchemy entity written for PostgreSQL dialect and it uses server_default=func.clock_timestamp()
:
row_created = sa.Column('row_created_', sa.DateTime(timezone=True), server_default=func.clock_timestamp(), nullable=False)
I also need to use it with SQLite, which throws an error:
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unknown function: clock_timestamp()
How do I make this work with SQLAlchemy? Define the server_default
only for SQLite? Conditionally add entire column only for specific dialect? Somehow make clock_timestamp()
known to SQLite (I don’t care what the function will do, as long as it does not fail)?
2
Answers
I got a tip from a colleague of mine, which I ended up using and it works fine for both postgresql as well as sqlite.
afaik it is the same as func.clock_timestamp(), but clock_timestamp() is postgresql-specific and now() is supoported by several DBs.
You can use:
text("CURRENT_TIMESTAMP")
for the server default:This will create a table with the following SQL:
PostgresSQL:
SQLite:
If you must use
clock_timestamp()
on PostgreSQL, you can set server defaultto:
postgres
sqlite
You can also use "default" for it to work on both databases.