Update:
After digging around a bit I kept the way of accessing the database, even though I would love to not do this. Instead of doing the query myself, I now use the finder:
$finder = \XF::finder('XF:Attachment');
$attachment = $finder->where('data_id', $dataId)->fetchOne();
$attachmentHash =...