Invalid default value for 'dateAdded'

I am having a stupid SQL issue that I cannot fix.

  ALTER TABLE `news` 
  ADD `dateAdded` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AUTO_INCREMENT,
  ADD PRIMARY KEY (`dateAdded`)

Mistake:

(#1067)Invalid default value for 'dateAdded' 

Can someone help me?

+69
sql mysql
Jan 25 2018-12-15T00:
source share
7 answers

CURRENT_TIMESTAMP valid only in TIMESTAMP fields. DATETIME fields must be left with either a default value of zero or no default value - the default values ​​must be a constant value, not the result of an expression.

relevant documents: http://dev.mysql.com/doc/refman/5.0/en/data-type-defaults.html

You can get around this by setting the post-insert trigger in the table to populate the "now" value for any new entries.

+110
Jan 25 '12 at 16:05
source share

CURRENT_TIMESTAMP is version specific and is now allowed for DATETIME columns since version 5.6.

See MySQL Docs .

+41
Sep 17 '14 at 10:44
source share

Also note when specifying DATETIME as DATETIME(3) or as in MySQL 5.7.x, you must also add the same value for CURRENT_TIMESTAMP(3) . If not, it will continue to throw "Invalid default value."

+7
Jul 06 '16 at 8:44
source share

I have mysql version 5.6.27 on my LEMP and CURRENT_TIMESTAMP, as the default works fine.

+1
Oct 06 '16 at 2:39 on
source share

mysql version 5.5 sets the default datetime value, since CURRENT_TIMESTAMP will present a report error, you can upgrade to version 5.6, set the default datetime value as CURRENT_TIMESTAMP

0
Dec 08 '16 at 6:55
source share

Change the type from datetime to timestamp and it will work! I had the same problem for mysql 5.5.56-MariaDB - MariaDB Server Hope this can help ... sorry if deprived

0
Nov 13 '17 at 16:23
source share

I had the same problem, after fixing my problem was resolved.

  • Select type as "TIMESTAMP"

  • DO NOT GO IN SEVERAL FULL LENGTHS / VALUES. KEEP MAN

  • Select CURRENT_TIMESTAMP as the default value.

I am using MySQL ver 5.5.56

0
Dec 07 '17 at 18:00
source share



All Articles