Friday, August 17, 2012

ORM tries to update zero records and fails

Here's one for the Doh! pile.  When using CFOrm, I generally create an init() function that gets run when an entity is created.  In this case, I had used the native id property from MySql to populate the key for the entity.  In the init method, I had set that to zero to make sure that a numeric value was created and added to that property.  Big mistake.

If the entity has a value for it's identity property, executing entitySave() attempts to update that record.  Since I had created a new entity, it doesn't exist when the save is attempted and throws an error.


The solution is simply to not pre-populate that property.  ORM expects that property to not exist when a save is executed.  When you do that, a new record is created and the entity is populated automatically with the new ID value.

Sometimes the error messages from ORM are less than intuitive.