PoorBooru/migrations/1_up.sql
2023-03-08 20:43:56 +00:00

39 lines
990 B
SQL

CREATE TABLE media(
media_id INTEGER PRIMARY KEY,
seaweedfs_fid BLOB (32) NOT NULL,
filename TEXT (256) NOT NULL,
content_type TEXT (256)
);
CREATE TABLE tags(
tag_id INTEGER PRIMARY KEY,
name TEXT (256) NOT NULL UNIQUE
);
CREATE TABLE media_tags(
media_id INTEGER NOT NULL,
tag_id INTEGER NOT NULL,
FOREIGN KEY (media_id) REFERENCES media (media_id) ON DELETE CASCADE,
FOREIGN KEY (tag_id) REFERENCES tags (tag_id) ON DELETE CASCADE,
PRIMARY KEY(media_id, tag_id)
);
CREATE VIEW tag_count_view AS SELECT
tags.tag_id AS tag_id,
tags.name AS name,
COUNT(media_tags.media_id) AS count
FROM tags
INNER JOIN media_tags USING (tag_id)
GROUP BY tag_id;
CREATE VIEW tagged_media_view AS SELECT
media.media_id AS media_id,
media.seaweedfs_fid AS media_seaweedfs_fid,
media.filename AS media_filename,
media.content_type AS media_content_type,
tags.tag_id AS tag_id,
tags.name AS tag_name
FROM media_tags
INNER JOIN media USING (media_id)
INNER JOIN tags USING (tag_id);