Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid default value for 'dateAdded'

Tags:

sql

mysql

I got a stupid problem with SQL that I can't fix.

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

Error:

(#1067)Invalid default value for 'dateAdded'

Can somebody help me?

like image 651
Robin Van den Broeck Avatar asked Jan 25 '12 15:01

Robin Van den Broeck


4 Answers

CURRENT_TIMESTAMP is only acceptable on TIMESTAMP fields. DATETIME fields must be left either with a null default value, or no default value at all - default values must be a constant value, not the result of an expression.

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

You can work around this by setting a post-insert trigger on the table to fill in a "now" value on any new records.

like image 178
Marc B Avatar answered Nov 08 '22 22:11

Marc B


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

See MySQL docs.

like image 39
David Soussan Avatar answered Nov 09 '22 00:11

David Soussan


Also do note when specifying DATETIME as DATETIME(3) or like on MySQL 5.7.x, you also have to add the same value for CURRENT_TIMESTAMP(3). If not it will keep throwing 'Invalid default value'.

like image 19
Torsten Ojaperv Avatar answered Nov 08 '22 22:11

Torsten Ojaperv


I had the same issue, following fix solved my problem.

  • Select Type as 'TIMESTAMP'

  • DON'T ENTER ANYTHING IN LENGTH/VALUES FIELD. KEEP IT BLANK

  • Select CURRENT_TIMESTAMP as Default value.

I am using MySQL ver 5.5.56

like image 2
Darshn Avatar answered Nov 08 '22 22:11

Darshn