0

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?

Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59
robel
  • 305
  • 3
  • 14

1 Answers1

2

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

old.save(failOnError: true)

to:

old.save(failOnError: true, flush: true)
Community
  • 1
  • 1
Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59