Mysql – thesql 8 datetime default throwing and error when forward engineering

MySQLmysql-8.0mysql-workbench

I am trying to create a schema by forward engineering a model eer diagram. But its giving an error on the datetime fields when using the default current_timestamp which is allowed in mysql 8 for datetime fields.

-- MySQL Workbench Forward Engineering

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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema test
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Schema test
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8 ;
USE `test` ;

-- -----------------------------------------------------
-- Table `test`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`users` (
  `user_id` CHAR(36) NOT NULL,
  `email` VARCHAR(225) NOT NULL,
  `password` VARCHAR(20) NOT NULL,
  `created_at` DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME(6) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME(6) NULL DEFAULT NULL,
  PRIMARY KEY (`user_id`)
ENGINE = InnoDB;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
ERROR: Error 1067: Invalid default value for 'created_at'

I don't know why this is. I thought it had something to do with the variables mysql workbench sets when forward engineering. But I can't figure out what it is as it looks like they are set in a normal way.

Is it due to the variables or the workbench? Also why are the variables used? Can I run the script without them or are they necessary? To be frank, I am a bit put off by the use of the work "OLD" in the variables. Is it using version 5.6 defaults? Because I want the all the new settings on mysql 8 to be used by default.

Best Answer

As @akina pointed out in the comments:

Default value datatype and column datatype MUST MATCH STRICTLY ! If you create DATETIME(6) column then you must use CURRENT_TIMESTAMP(6) default value. Test - add (6) to default value for created_at only - and the error message will point you to updated_at (the first syntax error detected)