Michael Plautz Michael Plautz - 6 months ago 38
MySQL Question

MySQL is not allowing ON UPDATE CURRENT_TIMESTAMP for a DATETIME field

I have seen a lot of related questions, but I cannot place my finger on this specific question:

I have a MySQL table with both a TIMESTAMP (for when the field was created) and a DATETIME (for each time the field gets updated). It looks like this:

CREATE TABLE 'vis' (
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
ENTRY VARCHAR(255),
AUTHOR VARCHAR(255),
CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT DATETIME ON UPDATE CURRENT_TIMESTAMP,
UPDATED_BY VARCHAR(255)
)


When I try this though, the error I am getting is:
(SQL Error: 1294 SQL State: HY000) - Invalid ON UPDATE clause for 'updated_at' field


Everywhere I have read (even on Stack Overflow) suggests I should be able to do this, yet I am getting this error. Perhaps there is another way to have a field that automatically updates the time each time I update it?

I am using MySQL Server 5.5.

Answer

DATETIME cannot use CURRENT_TIMESTAMP on update. Instead, change it to a TIMESTAMP.

Or, consider using a trigger for this situation: http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html