From: Mike Bayer Date: Mon, 29 Jun 2020 19:45:20 +0000 (-0400) Subject: Include DATETIME / DateTime with the MySQL TIMESTAMP examples X-Git-Tag: rel_1_3_19~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9b47fb31e3ff5a2c0aac04b3768d700041b61257;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Include DATETIME / DateTime with the MySQL TIMESTAMP examples To eliminate any remaining confusion, clarify that DATETIME (as well as DateTime) and TIMESTAMP are treated similarly with the MySQL dialect regarding ON UPDATE. Change-Id: I222522440706902d5d2d11e670e76f16000438e0 References: #5427 (cherry picked from commit 30f8843e97a49121323a4f75d9341f77c9935312) --- diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 0b4f7c7286..836aa3d198 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -670,8 +670,8 @@ with the ``unique=True`` setting present in the :attr:`_schema.Table.indexes` collection. -TIMESTAMP issues ------------------ +TIMESTAMP / DATETIME issues +--------------------------- .. _mysql_timestamp_onupdate: @@ -690,7 +690,8 @@ MySQL 5.6 introduced a new flag `explicit_defaults_for_timestamp #sysvar_explicit_defaults_for_timestamp>`_ which disables the above behavior, and in MySQL 8 this flag defaults to true, meaning in order to get a MySQL "on update timestamp" without changing this flag, the above DDL must be -rendered explicitly. +rendered explicitly. Additionally, the same DDL is valid for use of the +``DATETIME`` datatype as well. SQLAlchemy's MySQL dialect does not yet have an option to generate MySQL's "ON UPDATE CURRENT_TIMESTAMP" clause, noting that this is not a general @@ -718,6 +719,24 @@ parameter and pass a textual clause that also includes the ON UPDATE clause:: ) ) +The same instructions apply to use of the :class:`_types.DateTime` and +:class:`_types.DATETIME` datatypes:: + + from sqlalchemy import DateTime + + mytable = Table( + "mytable", + metadata, + Column('id', Integer, primary_key=True), + Column('data', String(50)), + Column( + 'last_updated', + DateTime, + server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") + ) + ) + + Even though the :paramref:`_schema.Column.server_onupdate` feature does not generate this DDL, it still may be desirable to signal to the ORM that this updated value should be fetched. This syntax looks like the following::