Invalid default value for 'create_date' timestamp field

后端 未结 16 1426
我寻月下人不归
我寻月下人不归 2020-11-30 16:49

I have the following sql create statement

mysql> CREATE  TABLE IF NOT EXISTS `erp`.`je_menus` (
    ->   `id` INT(11) NOT NULL AUTO_INCREMENT ,
    -&g         


        
相关标签:
16条回答
  • 2020-11-30 16:54

    Just Define following lines at top of your Database SQL file.

    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
    

    It is working for me.

    0 讨论(0)
  • 2020-11-30 16:56

    Change this:

    `create_date` TIMESTAMP NOT NULL DEFAULT  '0000-00-00 00:00:00',
    `update_date` TIMESTAMP NOT NULL DEFAULT  CURRENT_TIMESTAMP  ,
    

    To the following:

    `create_date` TIMESTAMP NOT NULL DEFAULT  CURRENT_TIMESTAMP ,
    `update_date` TIMESTAMP NOT NULL DEFAULT  CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
    
    0 讨论(0)
  • 2020-11-30 17:00

    To avoid this issue, you need to remove NO_ZERO_DATE from the mysql mode configuration.

    1. Go to 'phpmyadmin'.
    2. Once phpmyadmin is loaded up, click on the 'variables' tab.
    3. Search for 'sql mode'.
    4. Click on the Edit option and remove NO_ZERO_DATE (and its trailing comma) from the configuration.

    This is a very common issue in the local environment with wamp or xamp.

    0 讨论(0)
  • 2020-11-30 17:05

    You might like to examine the timezone setting on the MySql instance:

    mysql> show variables like 'time_zone';
    +---------------+--------+
    | Variable_name | Value  |
    +---------------+--------+
    | time_zone     | SYSTEM |
    +---------------+--------+
    

    in my case, I realised that the underlying system had it's timezone set to BST rather than UTC, and so in the create table the default of '1970-01-01 00:00:01' was being coerced back 1 hour, resulting in an invalid timestamp value.

    For me, I actually wanted the machine's timezone set to UTC, and that sorted me out. As I was running Centos/7, I simply did

    # timedatectl set-timezone UTC
    

    and restarted everything.

    0 讨论(0)
  • 2020-11-30 17:07

    You could just change this:

    create_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00',

    To something like this:

    create_date varchar(80) NOT NULL DEFAULT '0000-00-00 00:00:00',

    0 讨论(0)
  • 2020-11-30 17:08

    To disable strict SQL mode

    Create disable_strict_mode.cnf file at /etc/mysql/conf.d/
    

    In the file, enter these two lines:

    [mysqld]
    sql_mode=IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
    

    Finally, restart MySQL with this command:

    sudo service mysql restart
    
    0 讨论(0)
提交回复
热议问题