Desired behavior is to store timestamps in UTC computed always server side. In MySQL this is accomplished with the TIMESTAMP datatype;
|
mysql> create table tmp (timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP);
|
Query OK, 0 rows affected (0.03 sec)
|
|
mysql> describe jobs;
|
+------------+-------------+------+-----+-------------------+----------------+
|
| Field | Type | Null | Key | Default | Extra |
|
+------------+-------------+------+-----+-------------------+----------------+
|
|
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
|
+------------+-------------+------+-----+-------------------+----------------+
|
6 rows in set (0.00 sec)
|
which in SQLAlchemy translates to:
|
from sqlalchemy.sql import expression
|
from sqlalchemy.ext.compiler import compiles
|
from sqlalchemy.dialects.mysql import TIMESTAMP
|
|
class now(expression.FunctionElement):
|
type = TIMESTAMP
|
|
@compiles(now, 'mysql')
|
def mysql_now(element, compiler, **kw):
|
return "CURRENT_TIMESTAMP()"
|
|
|
class TestModel(db.Model):
|
__tablename__ = 'tmp'
|
timestamp = db.Column(db.TIMESTAMP, nullable=False, server_default=now())
|
Desired behavior is to store timestamps in UTC computed always server side. In MySQL this is accomplished with the TIMESTAMP datatype;
mysql> create table tmp (timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP);
Query OK, 0 rows affected (0.03 sec)
mysql> describe jobs;
+------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+-------------------+----------------+
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
+------------+-------------+------+-----+-------------------+----------------+
6 rows in set (0.00 sec)
which in SQLAlchemy translates to: