]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Include DATETIME / DateTime with the MySQL TIMESTAMP examples
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 29 Jun 2020 19:45:20 +0000 (15:45 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 29 Jun 2020 19:45:20 +0000 (15:45 -0400)
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

lib/sqlalchemy/dialects/mysql/base.py

index e64bd97cc6df35b69b4f8a8472e58e0ba39ff927..d3d7a8ccebcebfbaf2039d631aa0e20282abf505 100644 (file)
@@ -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::