users
Authors and commenters.
A blog needs surprisingly few tables: people, the things they write, the replies those draw, and a way to label posts by topic. This schema also works as a starting point for a news site or a simple CMS.
Authors and commenters.
Articles, each written by one user.
Replies on a post, each written by a user.
Labels for topics, such as “databases” or “travel”.
A junction table connecting posts and tags. A post can have many tags and a tag can label many posts.
posts.author_id references users.id: one row in users can have many rows in posts.comments.post_id references posts.id: one row in posts can have many rows in comments.comments.user_id references users.id: one row in users can have many rows in comments.post_tags is a junction table between posts and tags: many-to-many.Copy it into your database, or open it in Crowsfoot to edit the diagram, add notes, and export again.
CREATE TABLE users (
id bigint PRIMARY KEY,
username varchar(40) NOT NULL,
email varchar(255) NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE posts (
id bigint PRIMARY KEY,
author_id bigint NOT NULL REFERENCES users (id),
title varchar(200) NOT NULL,
body text NOT NULL,
published_at timestamptz
);
CREATE TABLE comments (
id bigint PRIMARY KEY,
post_id bigint NOT NULL REFERENCES posts (id),
user_id bigint NOT NULL REFERENCES users (id),
body text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE tags (
id bigint PRIMARY KEY,
name varchar(50) NOT NULL
);
CREATE TABLE post_tags (
post_id bigint NOT NULL REFERENCES posts (id),
tag_id bigint NOT NULL REFERENCES tags (id),
PRIMARY KEY (post_id, tag_id)
);
New to ER diagrams? Read the crow’s foot notation guide.