Search code examples
hibernategrails

Grails/Hibernate: Field "version" doesn't have a default value


I added a new domain class to my project, and when call the method to add a new instance to the table, throw a strange error.


This is the call: (a simple save and print errors if not)

if (!distributor.save()) {
    flash.error = message(code: 'unexpectedError')
    distributor.errors.each {
        println it  
    }
}

This is the stacktrace: (only the caused by portion)

Caused by: java.sql.SQLException: Field 'version' doesn't have a default value

Does someone got this error? I don't understand why this is happening because version is a auto-generated field by Grails/Hibernate framework.

I tried to delete the table and recreate it but still getting the same error. Could that be an bug? I don't think that i am doing wrong because is not my first table or relationship in the project.

Any help will be appreciated.


Solution

  • I found a solution that is not the most efficient but solved my problem.

    That looks like a Grails/Hibernate bug, that happens sometimes when you add a domain class, or modify it or their relationships while the project is already running.


    In this question they ask about the same error, but with the field id. I tried this solution and work:

    1. Drop the table in question.
    2. Make a backup of the database.
    3. Drop the database
    4. Recreate the database from the backup
    5. Run the project to create the table in question

    Step 1 and 2 are not needed if you already have a backup.


    The problem of this solution is the time if your database is big. The main take around 20-30 min to do the backup or recreate the database from the backup.