Backport of 902e763232b023dc0892d119c510d38449b6bd2d (Fix for bad data inserted as a result of being called with an incorrect typeId)