rheldt
unread,Oct 10, 2011, 10:28:36 AM10/10/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to cf-orm-dev
Greetings everyone!
I'm having an issue with entityDelete() I'm trying to hammer out and
hopefully someone can point me in the right direction. Let's say I
have a CFC for handling songs, of which a song can be associated with
a category:
Song.cfc:
<cfproperty name="SongID" type="numeric" ormtype="integer"
notnull="true" fieldtype="id" generator="identity" />
<cfproperty name="SongCategory" cfc="SongCategory" fieldtype="many-to-
one" fkcolumn="SongCategoryID" />
<cfproperty name="Name" type="string" ormtype="string" length="100" />
<cfproperty name="Author" type="string" ormtype="string" length="100" /
>
SongCategory.cfc
<cfproperty name="SongCategoryID" type="numeric" ormtype="integer"
notnull="true" fieldtype="id" generator="identity" />
<cfproperty name="Name" type="string" ormtype="string" length="100" />
When someone deletes a song category, I'd like to be able to prevent
the user from doing so if its "in use" (associated with a song
record). Traditionally, I've done this with a try/catch around a SQL
DELETE statement. Since I'm using ORM, I figured I could put the try/
catch around entityDelete() to accomplish the same thing. For example:
transaction action="begin" {
.
.
local.objSongCategory =
entityLoad("SongCategory",SongCategoryID,true);
.
.
try {
entityDelete(local.objSongCategory);
} catch (Any e) {
transaction action="rollback";
}
}
However, when I attempt to delete a song category that's in use, I get
the following error:
Cannot delete or update a parent row: a foreign key constraint fails
(`mydsn`.`songs`, CONSTRAINT `songs_ibfk_3` FOREIGN KEY
(`SongCategoryID`) REFERENCES `songcategories` (`SongCategoryID`))
Yes, technically that error is correct... if the record was to be
deleted, there would be a constraint violation. However, error is
being thrown despite being inside a try/catch. Does anyone know why?
Am I doing something wrong?