Generator-jhipster: Invalid default value for required ZonedDateTime.

Created on 11 May 2017  ·  7Comments  ·  Source: jhipster/generator-jhipster

Overview of the issue

Error occurs when field of type ZonedDateTime is marked as required in JDL Studio.

Motivation for or Use Case

Generator should provide valid default values for every type.

Reproduce the error

Create entity in JDL studio with required field of ZonedDateTime type.
Import to jhipster project with yo jhipster:import-jdl
Run application with /mvnw command.

Error:
`2017-05-11 13:42:50.997 ERROR 29125 --- [uler-Executor-1] liquibase : classpath:config/liquibase/master.xml: classpath:config/liquibase/changelog/20170510180807_added_entity_Event.xml::20170510180807-1::jhipster: Change Set classpath:config/liquibase/changelog/20170510180807_added_entity_Event.xml::20170510180807-1::jhipster failed. Error: Invalid default value for 'jhi_stop' [Failed SQL: CREATE TABLE CScheduler.event (id BIGINT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, jhi_start timestamp NOT NULL, jhi_stop timestamp NOT NULL, CONSTRAINT PK_EVENT PRIMARY KEY (id))]
2017-05-11 13:42:51.004 ERROR 29125 --- [uler-Executor-1] i.g.j.c.liquibase.AsyncSpringLiquibase : Liquibase could not start correctly, your database is NOT ready: Migration failed for change set classpath:config/liquibase/changelog/20170510180807_added_entity_Event.xml::20170510180807-1::jhipster:
Reason: liquibase.exception.DatabaseException: Invalid default value for 'jhi_stop' [Failed SQL: CREATE TABLE CScheduler.event (id BIGINT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, jhi_start timestamp NOT NULL, jhi_stop timestamp NOT NULL, CONSTRAINT PK_EVENT PRIMARY KEY (id))]

liquibase.exception.MigrationFailedException: Migration failed for change set classpath:config/liquibase/changelog/20170510180807_added_entity_Event.xml::20170510180807-1::jhipster:
Reason: liquibase.exception.DatabaseException: Invalid default value for 'jhi_stop' [Failed SQL: CREATE TABLE CScheduler.event (id BIGINT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, jhi_start timestamp NOT NULL, jhi_stop timestamp NOT NULL, CONSTRAINT PK_EVENT PRIMARY KEY (id))]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:619)
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:51)
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:79)
at liquibase.Liquibase.update(Liquibase.java:214)
at liquibase.Liquibase.update(Liquibase.java:192)
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:431)
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:388)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:82)
at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:64)
at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: liquibase.exception.DatabaseException: Invalid default value for 'jhi_stop' [Failed SQL: CREATE TABLE CScheduler.event (id BIGINT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, jhi_start timestamp NOT NULL, jhi_stop timestamp NOT NULL, CONSTRAINT PK_EVENT PRIMARY KEY (id))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113)
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1277)
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1259)
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:582)
... 12 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Invalid default value for 'jhi_stop'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2497)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2455)
at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:839)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:739)
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:307)
... 17 common frames omitted

`

Related issues

None.

Suggest a Fix

Provide valid default value for ZonedDateTime.

JHipster Version(s)
JHipster configuration

`##### JHipster Version(s)

[email protected] /Users/ErnestChechelski/Desktop/jHipsterCScheduler
└── [email protected] 

JHipster configuration, a .yo-rc.json file generated in the root folder
{
  "generator-jhipster": {
    "promptValues": {
      "packageName": "ernestchechelski.cscheduler",
      "nativeLanguage": "pl"
    },
    "jhipsterVersion": "4.4.1",
    "baseName": "CScheduler",
    "packageName": "ernestchechelski.cscheduler",
    "packageFolder": "ernestchechelski/cscheduler",
    "serverPort": "8080",
    "authenticationType": "jwt",
    "hibernateCache": "ehcache",
    "clusteredHttpSession": false,
    "websocket": "spring-websocket",
    "databaseType": "sql",
    "devDatabaseType": "mysql",
    "prodDatabaseType": "mysql",
    "searchEngine": false,
    "messageBroker": false,
    "serviceDiscoveryType": "eureka",
    "buildTool": "maven",
    "enableSocialSignIn": true,
    "jwtSecretKey": "XDDD",
    "clientFramework": "angular1",
    "useSass": true,
    "clientPackageManager": "yarn",
    "applicationType": "monolith",
    "testFrameworks": [],
    "jhiPrefix": "jhi",
    "enableTranslation": true,
    "nativeLanguage": "pl",
    "languages": [
      "pl",
      "en"
    ]
  }
}
Entity configuration(s) entityName.json files generated in the .jhipster directory

Contributor.json

{
    "fluentMethods": true,
    "relationships": [
        {
            "relationshipType": "many-to-one",
            "relationshipName": "owner",
            "otherEntityName": "user",
            "otherEntityField": "login"
        }
    ],
    "fields": [
        {
            "fieldName": "alias",
            "fieldType": "String",
            "fieldValidateRules": [
                "required",
                "minlength"
            ],
            "fieldValidateRulesMinlength": 5
        }
    ],
    "changelogDate": "20170510180808",
    "entityTableName": "contributor",
    "dto": "no",
    "pagination": "no",
    "service": "no"
}

Event.json

{
    "fluentMethods": true,
    "relationships": [],
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String",
            "fieldValidateRules": [
                "required",
                "minlength"
            ],
            "fieldValidateRulesMinlength": 3
        },
        {
            "fieldName": "start",
            "fieldType": "ZonedDateTime",
            "fieldValidateRules": [
                "required"
            ]
        },
        {
            "fieldName": "stop",
            "fieldType": "ZonedDateTime",
            "fieldValidateRules": [
                "required"
            ]
        }
    ],
    "changelogDate": "20170510180807",
    "entityTableName": "event",
    "dto": "no",
    "pagination": "no",
    "service": "no"
}

Group.json

{
    "fluentMethods": true,
    "relationships": [],
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String",
            "fieldValidateRules": [
                "required",
                "minlength"
            ],
            "fieldValidateRulesMinlength": 3
        }
    ],
    "changelogDate": "20170510180809",
    "entityTableName": "group",
    "dto": "no",
    "pagination": "no",
    "service": "no"
}

Plan.json

{
    "fluentMethods": true,
    "relationships": [],
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String",
            "fieldValidateRules": [
                "required",
                "minlength"
            ],
            "fieldValidateRulesMinlength": 3
        }
    ],
    "changelogDate": "20170510180810",
    "entityTableName": "plan",
    "dto": "no",
    "pagination": "no",
    "service": "no"
}
Browsers and Operating System

java version "1.8.0_77"
Java(TM) SE Runtime Environment (build 1.8.0_77-b03)
Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)

git version 2.10.1 (Apple Git-78)

node: v7.10.0

npm: 4.2.0

bower: 1.8.0

gulp:
[13:55:54] CLI version 1.3.0
[13:55:54] Local version 3.9.1

yeoman: 1.8.5

yarn: 0.23.4`

Entity configuration(s) entityName.json files generated in the .jhipster directory

`//User

relationship ManyToOne {
Contributor{owner(login)} to User

}
//Contributors
entity Contributor {
alias String required minlength(5)
}

//Groups
entity Group {
name String required minlength(3)
}

//Plans
entity Plan {
name String required minlength(3)
}

//Events
entity Event {
name String required minlength(3),
start ZonedDateTime required,
stop ZonedDateTime required
}

`

Browsers and Operating System

OSX El Capitan 10.11.6 (15G1421)

  • [ X] Checking this box is mandatory (this is just to show you read everything)
waiting for information

All 7 comments

I didn't manage to reproduce. The application starts correctly on my side:

  • create the application with your .yo-rc.json
  • import your jdl
  • start mysql database with Docker
  • start the app with mvnw

Can you provide more information to reproduce your issue?
What database do you use? Real MySQL or our Docker compose file? Which version?

I was able to reproduce it by changing the mysql version to a later one than than our Docker image. We currently use v5.7.13 where the latest is v5.7.18. The reason we held the version back was because it doesn't allow two required timestamps in a table (see https://github.com/jhipster/generator-jhipster/pull/4038). This affects Instants as well as ZonedDateTime fields.

A workaround is to add --explicit_defaults_for_timestamp to your mysqld command or explicit_defaults_for_timestamp = 1 to your my.cnf

@ruddell : do you think we can add --explicit_defaults_for_timestamp to this line ? If yes, we should be able to upgrade from 5.7.13 to 5.7.18

@pascalgrimaud Yes I tested it with our docker-compose mysql.yml and adding it there fixes it.
One thing I'm worried about is that we aren't using the defaults which can cause out-of-the-box MySQL installations to fail, like in this issue. Maybe we should add something to the docs?

I agree for both: upgrading to 5.7.18 + add docs!

Yes +1 for upgrading, we can't stay on an older version for too long anyway.
And we have always added our customizations and bug corrections to other technologies, so that's normal for me.
@ruddell are you OK to do a PR?

I was able to reproduce using the entity generator (liquibase changelog results in an error).

jhipster version: 4.9.0
DBMS: mysql:5.7.19
entity :

{
    "fluentMethods": true,
    "relationships": [],
    "fields": [
        {
            "fieldName": "end",
            "fieldType": "ZonedDateTime",
            "fieldValidateRules": [
                "required"
            ]
        }
    ],
    "changelogDate": "20171006153741",
    "dto": "no",
    "service": "no",
    "entityTableName": "lesson",
    "jpaMetamodelFiltering": false,
    "pagination": "no"
}

error: Reason: liquibase.exception.DatabaseException: Invalid default value for 'jhi_end'

It seems MYSQL type timestamp needs an explicit default value by default.

I was using a docker-compose if it can help you :

version: '2'
services:
  mysql:
    image: mysql:5.7.19
    container_name: scheduler-mysql
    networks:
      default:
        ipv4_address: 10.10.10.1
    environment:
        MYSQL_ROOT_PASSWORD: password
    volumes:
        - ./persistence:/var/lib/mysql
  pma:
    image: phpmyadmin/phpmyadmin
    container_name: scheduler-pma
    links:
      - mysql:db
    networks:
      default:
        ipv4_address: 10.10.10.1
networks:
    default:
        driver: bridge
        ipam:
            driver: default
            config:
            - subnet: 10.10.10.0/24
              gateway: 10.10.10.254
Was this page helpful?
0 / 5 - 0 ratings

Related issues

trajakovic picture trajakovic  ·  4Comments

sdoxsee picture sdoxsee  ·  4Comments

pascalgrimaud picture pascalgrimaud  ·  4Comments

ahmedeldeeb25 picture ahmedeldeeb25  ·  3Comments

chegola picture chegola  ·  4Comments