h icon indicating copy to clipboard operation
h copied to clipboard

default overrides server_default in sqlalchemy column defaults

Open hmstepanek opened this issue 6 years ago • 0 comments
trafficstars

It's best practice to let the database provide defaults to timestamp columns by configuring server_default.

registered_date = sa.Column(
    sa.TIMESTAMP(timezone=False),
    default=datetime.datetime.utcnow,
    server_default=sa.func.now(),
    nullable=False,
)

It appears developers in H intended to do this in the code by setting server_default on certain columns however, by also configuring default, sqlalchemy will override the server_default behavior. default is added by sqlalchemy upon sending the insert query to the db thus providing a value so that the server_default does not take place. The default needs to be removed so that all datetime columns get their default datetime from the db and not the python server. This eliminates inconsistencies in timestamps due to clock drift between the server and the db and also reduces the amount of data we send across the network to the db. See slack discussion here.

hmstepanek avatar Mar 08 '19 00:03 hmstepanek