PoorBooru/migrations/1_up.sql

36 lines
888 B
SQL

CREATE TABLE media(
media_id INTEGER PRIMARY KEY,
seaweedfs_fid TEXT (33) 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
-- tags.tag_id AS tag_id,
-- tags.name AS tag_name,
-- media.media_id AS media_id
-- FROM media
-- INNER JOIN media_tags USING (media_id)
-- INNER JOIN tags USING (tag_id);