Why won't my Grails application run when I set dbCreate = "update"

191 views Asked by At

My Grails application and bootstrap work fine when dbCreate="create", but when I change it to dbCreate="update", I get object create validation errors in bootstrap. I'd just like my data to persist when I restart the application. From the error message, it appears I'm violating a unique constraints. Maybe the database isn't getting purged on restart? I've tried "create-drop" Here is the code and error message below. Any insight is appreciated.

development {
    dataSource {
        dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
        url = "jdbc:h2:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;"
    }
}


class BootStrap {

    def init = { servletContext ->

        def adminRole = new com.testapp.Role(authority: 'ROLE_ADMIN').save(failOnError: true)
        def userRole = new com.testapp.Role(authority: 'ROLE_USER').save(failOnError: true)
}

Message:

Validation Error(s) occurred during save(): - Field error in object 'com.testapp.Role' on field 'authority': rejected value [ROLE_ADMIN]; codes [com.testapp.Role.authority.unique.error.

default message [Property [{0}] of class [{1}] with value [{2}] must be unique

1

There are 1 answers

2
Vivek Sadh On

I think you must have already created the Role with Authority "ROLE_ADMIN" or "ROLE_USER". The second time you are running with update gives an error because of unique constraint. An attempt is being made to create role with same Authority names and it throws error.

You should apply a condition such that if a role already exist, you should not try to create the same again.