ERROR 1067 (42000): Invalid default value for 'created_at'

前端 未结 14 1672
-上瘾入骨i
-上瘾入骨i 2020-12-04 07:31

When I tried to alter the table it showed the error:

ERROR 1067 (42000): Invalid default value for \'created_at\'

I googled for this error

相关标签:
14条回答
  • 2020-12-04 08:11

    Run this query:

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

    it works for me

    0 讨论(0)
  • 2020-12-04 08:11

    For Mysql8.0.18:

    CURRENT_TIMESTAMP([fsp])
    

    Remove "([fsp])", resolved my problem.

    0 讨论(0)
  • 2020-12-04 08:13

    As mentioned in @Bernd Buffen's answer. This is issue with MariaDB 5.5, I simple upgrade MariaDB 5.5 to MariaDB 10.1 and issue resolved.

    Here Steps to upgrade MariaDB 5.5 into MariaDB 10.1 at CentOS 7 (64-Bit)

    1. Add following lines to MariaDB repo.

      nano /etc/yum.repos.d/mariadb.repo and paste the following lines.

    [mariadb]
    name = MariaDB
    baseurl = http://yum.mariadb.org/10.1/centos7-amd64
    gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
    gpgcheck=1

    1. Stop MariaDB, if already running service mariadb stop
    2. Perform update

      yum update

    3. Starting MariaDB & Performing Upgrade

      service mariadb start

      mysql_upgrade

    Everything Done.

    Check MariaDB version: mysql -V


    NOTE: Please always take backup of Database(s) before performing upgrades. Data can be lost if upgrade failed or something went wrong.

    0 讨论(0)
  • 2020-12-04 08:14

    You can do it like this:

     CREATE TABLE `ttt` (
      `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
      `t1` TIMESTAMP  NULL DEFAULT '0000-00-00 00:00:00',
      `t2` TIMESTAMP  NULL DEFAULT '0000-00-00 00:00:00',
      `t3` TIMESTAMP  NULL DEFAULT '0000-00-00 00:00:00',
      `t4` TIMESTAMP  NULL DEFAULT 0,
      PRIMARY KEY (`id`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8;
    
    • Because the TIMESTAMP value is stored as Epoch Seconds, the timestamp value '1970-01-01 00:00:00' (UTC) is reserved since the second #0 is used to represent '0000-00-00 00:00:00'.
    • In MariaDB 5.5 and before there could only be one TIMESTAMP column per table that had CURRENT_TIMESTAMP defined as its default value. This limit has no longer applied since MariaDB 10.0.

    see: https://mariadb.com/kb/en/mariadb/timestamp/

    sample

    MariaDB []> insert into ttt (id) VALUES (1),(2),(3);
    Query OK, 3 rows affected (0.01 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    MariaDB []> select * from ttt;
    +----+---------------------+---------------------+---------------------+---------------------+
    | id | t1                  | t2                  | t3                  | t4                  |
    +----+---------------------+---------------------+---------------------+---------------------+
    |  1 | 0000-00-00 00:00:00 | 2000-01-01 12:01:02 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  2 | 0000-00-00 00:00:00 | 2000-01-01 12:01:02 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    |  3 | 0000-00-00 00:00:00 | 2000-01-01 12:01:02 | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
    +----+---------------------+---------------------+---------------------+---------------------+
    3 rows in set (0.00 sec)
    
    MariaDB []>
    
    0 讨论(0)
  • 2020-12-04 08:15

    The problem is because of sql_modes. Please check your current sql_modes by command:

    show variables like 'sql_mode' ; 
    

    And remove the sql_mode "NO_ZERO_IN_DATE,NO_ZERO_DATE" to make it work. This is the default sql_mode in mysql new versions.

    You can set sql_mode globally as root by command:

    set global sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
    
    0 讨论(0)
  • 2020-12-04 08:16

    Try and run the following command:

    ALTER TABLE `investments` 
    MODIFY created_at TIMESTAMP 
    DEFAULT CURRENT_TIMESTAMP 
    NOT NULL;
    

    and

    ALTER TABLE `investments` 
    MODIFY updated_at TIMESTAMP 
    DEFAULT CURRENT_TIMESTAMP 
    NOT NULL;
    

    The reason you are getting this error is because you are not setting a default value for the created_at and updated_at fields. MySQL is not accepting your command since the values for these columns cannot be null.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题