7

i am getting error in my database. i am encountering invalid default value for timestamp.

here's my database:

CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
  `text` varchar(10000) NOT NULL,
  `threadId` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;



CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `color` varchar(10) DEFAULT '#00bcd4',
  `icon` varchar(100) NOT NULL DEFAULT 'https://mymonas.com/forum/category_icon/ic_question.png'
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Denver Bautista
  • 111
  • 1
  • 1
  • 7
  • Can I suggest you change your character set to `UTF8mb4`, as this will sidestep issues with wyrd characters in the future. [read more here](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Martin Apr 24 '16 at 11:23
  • 1
    same problem i got ,#1067 - Invalid default value for 'timestamp' – Denver Bautista Apr 25 '16 at 02:38
  • Oh yeah, it's an aside, not a solution in itself. – Martin Apr 25 '16 at 09:43
  • Use `TIMESTAMP` data type or update your MySQL server. – Paul Spiegel Sep 23 '16 at 22:12
  • Does this answer your question? [Mysql datetime DEFAULT CURRENT\_TIMESTAMP error](https://stackoverflow.com/questions/23153822/mysql-datetime-default-current-timestamp-error) – miken32 Dec 05 '20 at 19:25

5 Answers5

8

I was having the same problem, I changed type from "datetime" to "timestamp" and It worked. I have mysql 5.5.52.

Mysql_error

Raul A.
  • 81
  • 1
  • 5
4

I have the same issue in sql_mode.

Make query:

show variables like 'sql_mode' ; 

You need to remove the "NO_ZERO_IN_DATE,NO_ZERO_DATE" from sql_mode.

SET sql_mode = '';
Max Sherbakov
  • 1,817
  • 16
  • 21
3

Use CURRENT_TIMESTAMP() instead CURRENT_TIMESTAMP

i.e.

CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL,
  `text` varchar(10000) NOT NULL,
  `threadId` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `dateCreated` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
  `timestamp` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),
  `isModified` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB AUTO_INCREMENT=171 DEFAULT CHARSET=latin1;

Now() works as well

Pavel Zimogorov
  • 1,387
  • 10
  • 24
1

From the MySQL 5.5 manual:

"You cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column."

The changes in MYSQL 5.6.x that allow the functionality are documented here:

"As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table."

So, this means you are using an older version of mysql, either you can use datetime data type of upgrade your mysql version

Krish
  • 379
  • 2
  • 8
1

Answered by @max Sherbakov worked but I think its risky,

if you execute SET sql_mode = ''; query.

Because if you or other users SET any different variables in sql_mode

like NO_ENGINE_SUBSTITUTION check other SQL MODES

by changing sql_mode values in my.ini file

OR

using SET sql_mode = 'YOUR_VARIABLE_LIST'; query

it worked for you current situation

but create problem in other projects.

  • To view current sql mode use following query

    show variables like 'sql_mode' ;

Satish
  • 696
  • 1
  • 11
  • 22