Mikey
Mikey

Reputation: 4742

Grails: situation where .validate() works but .save() will fail

If a domain class will validate can I be assured it will save (assuming nothing super-drastic like the database is down)? More explicitly, under which scenarios will an object pass validation but throw an error on save.

Upvotes: 2

Views: 530

Answers (2)

Dónal
Dónal

Reputation: 187529

under which scenarios will an object pass validation but throw an error on save.

Domain class constraints can check anything, so if "the thing they check" changes between the calls to validate() and save(), then it's entirely possible that the former will succeed and the latter fails.

Here's a very simple such example

  • Book.ISBN has a unique contraint
  • myBook.validate() is called and passes
  • another book with the same ISBN as myBook is saved
  • myBook.save() fails because the unique constraint on ISBN now fails

Upvotes: 3

doelleri
doelleri

Reputation: 19682

Save will throw an error if your database has additional constraints that don't allow the insert or update to succeed. If your database doesn't have any additional constraints and barring any sort of infrastructure outages, I can't think of any reason save() would fail if validate() is true.

Upvotes: 3

Related Questions