0

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.

11
  • why is Dateime(6) at all, and especially what do you want to achieve with it
    – nbk
    Commented Aug 26, 2020 at 8:23
  • @nbk its fractional seconds. The mysql workbench set it by default when selecting datetime. I know 6 is a bit much, I was gonna reset it to 2. But there are a lot of tables each with 3-4 datetime fields. So I decided to check the script before going into each table and editing them, so as if there are any more errors, I can do all of the editing in one go.
    – Va_M
    Commented Aug 26, 2020 at 8:31
  • this runs in 8.0.21 with out any problem, se if you got the latest verwsion
    – nbk
    Commented Aug 26, 2020 at 8:50
  • Yeah I have the same version 8.0.21. I am running a docker container. workbench version is 8.0. I ran again after your validation, but it gave the same error. But as you pointed out the fractional seconds part, I reran the query without it and it worked. But when I added it again, it again gave an error. So it seems to me the issue is with the fractional part of datetime. No idea why that is the case. If you have any idea pls let me know. Also does your code run with the frational part? or without it? Thanks for the reply and effort. Appreciate it.
    – Va_M
    Commented Aug 26, 2020 at 8:58
  • i feared as much, get rid of the (6) if you don't need it
    – nbk
    Commented Aug 26, 2020 at 9:06

1 Answer 1

0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.