Project: Okul Database · Hub: Okul Database — Reference

Reference: media_types & media_roles

The media table uses a polymorphic pattern:

  • media.media_type_id → which entity type owns this file
  • media.media_role_id → what role/purpose the file serves for that entity
  • media.row_id → the ID of the owning entity

media_types

idnameRelated Table
1Articlearticles
2Schoolschools
3Userusers
4Collegecolleges
5Gallerygalleries
6Announcementannouncements
7Scholarshipscholarships
8CustomerAgreementcustomer_agreements
9Achievementsachievements
10AnnouncementEventsannouncement_events
11AgreementDocumentsagreement_documents

media_roles

idrolemedia_type_idDescription
1cover1 (Article)Article cover image
2content1 (Article)Article inline image
3profile2 (School)School profile photo
4gallery2 (School)School gallery photo
5profile3 (User)User avatar
6thumb4 (College)College thumbnail
7cover4 (College)College cover image
8content4 (College)College content image
9listing5 (Gallery)Gallery listing image
10content5 (Gallery)Gallery content image
11content6 (Announcement)Announcement image
12Kapak ve İçerik7 (Scholarship)Scholarship cover + content
13İçerik7 (Scholarship)Scholarship content image
14video2 (School)School video
15agreement8 (CustomerAgreement)Signed agreement document
16logo2 (School)School logo
17announcement-achievements9Achievement announcement
18announcement-events10Event announcement
19agreement-documents11Agreement supporting docs

Fetching School Media

-- All school photos (profile + gallery)
SELECT m.*, mt.name as type_name, mr.role
FROM media m
JOIN media_types mt ON mt.id = m.media_type_id
JOIN media_roles mr ON mr.id = m.media_role_id
WHERE m.media_type_id = 2        -- School
  AND m.row_id = <school_id>
  AND m.deleted_at IS NULL
ORDER BY mr.role, m.created_at;
 
-- Only profile photos
WHERE m.media_type_id = 2 AND m.media_role_id = 3 AND m.row_id = <school_id>
 
-- Count schools with at least one gallery photo
SELECT COUNT(DISTINCT row_id) as schools_with_gallery
FROM media
WHERE media_type_id = 2 AND media_role_id = 4 AND deleted_at IS NULL;