robel
robel

Reputation: 315

Grails unique validation exception with two consecutive saves

Let's assume name and id are the only fields on my user table and I have an unique constraints set for both. So if I have a value with id 2, name "robel", why is the below code giving me validation exception?

User old = UserFindByName("robel")
old.setName("changed")
old.save(failOnError: true)
User neew = new User(name:"robel")
neew.save(failOnError: true)

I tried setting breakpoints and checking if the value of old is changed before saving neew,and i am sure its changed but the last line is giving me an exception saying name:"robel" needs to be unique .Why is this happening?

Upvotes: 0

Views: 57

Answers (1)

Michal_Szulc
Michal_Szulc

Reputation: 4177

You should flush changes before adding new one instance. Change:

old.save(failOnError: true)

to:

old.save(failOnError: true, flush: true)

Upvotes: 2

Related Questions