From the looks of it, your $xclasses object contains new related objects. So ORM will insert that first.
The fact that you delete a parent doesn't directly mean you want to delete all related objects too. You need to enable cascade_delete on the relation if you want to automatically delete related child objects of that relation.
No, you define it on the relation. So when you delete the parent, it will delete associated children.
If you don't want that, it will have to reset the foreign key of all related childeren to NULL (since you are deleting the record the foreign key points to), which with your constraint will be a problem too.