Reputation: 1196
I am working in Grails. I am writing a code that deletes the account of a particular logged in user once he has clicked on the Delete link. Now this is my code found in my controller,
def delete = {
def account = springSecurityService.getCurrentUser()
def registrant = Registrant.findByAccount(account)
def loggeduser = registrant.account.username
RegistrantEligibilityInformation.executeUpdate("delete RegistrantEligibilityInformation as rei where rei.registrant in (select reg from Registrant as reg where reg.account.username in(:loggeduser))",[loggeduser:loggeduser])
RegistrantEducationInformation.executeUpdate("delete RegistrantEducationInformation as reduc where reduc.registrant in (select reg from Registrant as reg where reg.account.username in(:loggeduser))",[loggeduser:loggeduser])
Registrant.executeUpdate("delete Registrant as reg where reg.account.username in(:loggeduser)",[loggeduser:loggeduser])
AccountRole.executeUpdate("delete AccountRole as actrole where actrole.account.username in(:loggeduser)",[loggeduser:loggeduser])
Account.executeUpdate("delete Account as act where act.username in(:loggeduser)",[loggeduser:loggeduser])
toolsService.deletion(registrant)
}
I have five domain classes here which are the RegistrantEligibilityInformation, RegistrantEducationInformation, Registrant, AccountRole, and Account. Everytime I run my program, I got an error message like this:
URI: /user/profile/delete
Class: org.postgresql.util.PSQLException
Message: ERROR: column reference "id" is ambiguous Position: 49
...which pertains to this code:
RegistrantEligibilityInformation.executeUpdate("delete RegistrantEligibilityInformation as rei where rei.registrant in (select reg from Registrant as reg where reg.account.username in(:loggeduser))",[loggeduser:loggeduser])
...and I think to the rest of my codes too. I don't know what to do, as I am still new to Grails.
Upvotes: 0
Views: 172
Reputation: 75671
Use in
when you have a collection of items, not a single one - use =
when it must match. So in
makes sense for the subqueries, but not the loggeduser matches.
I'm not sure why Hibernate doesn't support the subquery you're using, but it works if you match on the id instead of the whole object:
RegistrantEligibilityInformation.executeUpdate(
"delete RegistrantEligibilityInformation as rei where rei.registrant.id in (" +
"select reg.id from Registrant as reg where reg.account.username=:loggeduser)",
[loggeduser:loggeduser])
RegistrantEducationInformation.executeUpdate(
"delete RegistrantEducationInformation as reduc where reduc.registrant.id in (" +
"select reg.id from Registrant as reg where reg.account.username=:loggeduser)",
[loggeduser:loggeduser])
Registrant.executeUpdate(
"delete Registrant as reg where reg.account.username=:loggeduser",
[loggeduser:loggeduser])
AccountRole.executeUpdate(
"delete AccountRole as actrole where actrole.account.username=:loggeduser)",
[loggeduser:loggeduser])
Account.executeUpdate(
"delete Account as act where act.username=:loggeduser)",
[loggeduser:loggeduser])
Upvotes: 2