Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid default value for 'timestamp'

Tags:

database

mysql

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;
like image 354
Denver Bautista Avatar asked Apr 20 '16 14:04

Denver Bautista


People also ask

What is the default value for timestamp?

A TIMESTAMP column that permits NULL values does not take on the current timestamp at insert time except under one of the following conditions: Its default value is defined as CURRENT_TIMESTAMP and no value is specified for the column.

What is current timestamp in MySQL?

MySQL CURRENT_TIMESTAMP() Function The CURRENT_TIMESTAMP() function returns the current date and time. Note: The date and time is returned as "YYYY-MM-DD HH-MM-SS" (string) or as YYYYMMDDHHMMSS. uuuuuu (numeric).

What is difference between timestamp and datetime in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.


2 Answers

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

Mysql_error

like image 72
Raul A. Avatar answered Sep 22 '22 04:09

Raul A.


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

like image 34
Pavel Zimogorov Avatar answered Sep 23 '22 04:09

Pavel Zimogorov