curious
curious

Reputation: 95

TIMESTAMP default value error

I exported SQL script with workbench to phpMyAdmin and I get an error because of TIMESTAMP default value.

Here's a piece of code:

CREATE TABLE IF NOT EXISTS `test`.`is_users` (
  `count` INT(10) UNIQUE NOT NULL AUTO_INCREMENT,
  `active` ENUM('1', '0') NULL DEFAULT 1,
  `id` VARCHAR(36) UNIQUE NOT NULL,
  `created_at` TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:01',
  `updated_at` TIMESTAMP NOT NULL DEFAULT '1970-01-01 00:00:01',
  `deleted_at` TIMESTAMP NULL DEFAULT NULL,

And I get an error :

#1067 - Invalid default value for 'created_at'

To avoid an error, the default date should be for example 1970-01-01 01:01:01, it means that there cannot be zeros. How could I fix that? Thanks.

Upvotes: 4

Views: 2722

Answers (4)

curious
curious

Reputation: 95

Actually it is enough to delete this part of code:

 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';`

Upvotes: 0

coolnodje
coolnodje

Reputation: 807

On 5.7, you want to disable the TRADITIONAL in sql_mode in order to to be able to support '0000-00-00 00:00:00' in datetime and timestamp, or 0s in time as you need. For instance, by default on 5.7.10, SQL MODES are:

mysql> SELECT @@sql_mode;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                                                                               |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| STRICT_TRANS_TABLES,STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ALLOW_INVALID_DATES,ERROR_FOR_DIVISION_BY_ZERO,TRADITIONAL,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

You would do this by either setting sql_mode in your my.cf file or with command the line as follow:

SET sql_mode='ALLOW_INVALID_DATES';

You may want to keep some other modes on, and you should have a look on the documentation linked above. This is quite tricky as some modes are dependent on others.

Upvotes: 0

rostbot
rostbot

Reputation: 342

Try

SELECT FROM_UNIXTIME(1);

If your system runs in another timezone than UTC then you will not get '1970-01-01 00:00:01' as result. For example, my system is running in CET which results in '1970-01-01 01:00:01'. So this result should work as a default value for your table.

Upvotes: 1

Rick James
Rick James

Reputation: 142238

00:00:01 is valid for a time.

TIMESTAMP DEFAULT processing changed at 5.6.5. What version are you using? See http://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

I do not get an error in 5.6.12.

Solution: Upgrade to 5.6.

Upvotes: 0

Related Questions