-1

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

Siguza
  • 21,155
  • 6
  • 52
  • 89
Dave
  • 11
  • 4

1 Answers1

1

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.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
  • Role.findOrSave(authority: 'ROLE_ADMIN') – albertovilches Jun 12 '15 at 08:43
  • Thanks for the answer. You're correct, the data is still in the database form the previous run. I'm now running blank init {} in bootstrap on each startup. Seems to work well this way since I want to persist data...Thanks again. - Dave – Dave Jun 12 '15 at 18:00