Using the usual example for one-to-many ORM objects “Artist” and “Art”, I have an Art table with a column called ArtistID that is labeled as a foreign key of “Artist” (it uses, surprise, the primary key of the Artist table). When I then have a specific Artist (called “thisArtist”) and attempt to EntityDelete(“thisArtist”), I get an error:
The DELETE statement conflicted with the REFERENCE constraint “FK_Art_Artists”. The conflict occurred in database “ArtistTracker”, table “dbo.Art”, column ‘ArtistID’.
I feel pretty confident that I am doing something dumb, either with my setup of relations or perhaps with my syntax. Has anyone encountered this? I don’t see an example of how to EntityDelete() a specific Artist if it has foreign key constraints on entries in an Art table.
Your Artist will likely have an “arts” property, correct? And that points to an Art CFC, presumably. Your arts property probably looks like property name=”arts” cfc=”Art” fieldtype=”one-to-many”;
What’s missing is to tell ORM how to handle the case where you delete the Artist. In your case, you need to add a “cascade” attribute onto the arts property, like so: property… cascade=”all-delete-orphan”;
Then, when you entityDelete the Artist, Hibernate will also delete any orphaned Art elements that belonged to that artist.
When creating one-to-many relationships, you almost always want to specify cascade=”one-to-many” and inverse=”true”.