users
Accounts.
Social features boil down to a few relationships: people write posts, follow each other, and like what they read. The follows table is the interesting one, because both of its foreign keys point back at users.
Accounts.
Short messages written by one user.
Who follows whom. Both columns point at users, so this is a self-referencing many-to-many relationship.
Which user liked which post.
posts.user_id references users.id: one row in users can have many rows in posts.follows is a junction table: each row links one users row to another, so users can relate to many users in both directions.likes is a junction table between users and posts: 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,
handle varchar(30) NOT NULL,
display_name varchar(80) NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE posts (
id bigint PRIMARY KEY,
user_id bigint NOT NULL REFERENCES users (id),
body text NOT NULL,
created_at timestamptz NOT NULL
);
CREATE TABLE follows (
follower_id bigint NOT NULL REFERENCES users (id),
followee_id bigint NOT NULL REFERENCES users (id),
followed_at timestamptz NOT NULL,
PRIMARY KEY (follower_id, followee_id)
);
CREATE TABLE likes (
user_id bigint NOT NULL REFERENCES users (id),
post_id bigint NOT NULL REFERENCES posts (id),
liked_at timestamptz NOT NULL,
PRIMARY KEY (user_id, post_id)
);
New to ER diagrams? Read the crow’s foot notation guide.