Immune cells may play a role in causing cavities
newatlas.com
newatlas.com
It is established that immune cells are modulated by microbiota - and yes, there are oral microbiota, and there are even oral probiotic supplements one can take which seem to be effective for certain conditions. If you are interested in this topic, I'd recommend paying close attention to how the 2 main strains were discovered (S. Salivarius K12 and M18) - iirc. at least one of the two was first extracted from a child who had no cavities.
So eating something with lots sugar and not cleaning your mouth of all the remaining sugar could cause cavities. However, it's not the sugar, but the bacteria feeding off the sugar.