Here is a design of the persistence of a simple Social Media Platform. Currently, there are these tables:
- Users: Main table of the database that contains the information of the users registered in our application. The data that will be stored in this table will be the name
- Name: users
- Fields: id, name, username, password, email, bio, followers, following, picture.
- Primary key: id
- Posts: Database table with all the posts from all the users. Each post will contain the title, description, and the main content of the post.
- Name: posts
- Fields: id, title, picture, description, content, created_at, likes, user_id.
- Primary key: id
- Foreign key: user_id to table users
- Post Liked by Users: A table that defines the many to many relationship between multiple posts liked and the users that liked them.
- Name: posts_liked_users
- Fields: post_id, user_id
- Foreign key: post_id to table posts
- Foreign key: user_id to table users
- Follows. Table to be able to create a "following" relationship between users.
- Name: follows
- Fields: following_user_id, followed_user_id
- Foreign key: following_user_id to table users
- Foreign key: followed_user_id to table users
Here are the commands to create the tables
CREATE TABLE users(
id SERIAL PRIMARY KEY,
name VARCHAR (50) NOT NULL,
username VARCHAR (50) UNIQUE NOT NULL,
password VARCHAR (255) NOT NULL,
email VARCHAR (255) NOT NULL,
bio VARCHAR (255) NOT NULL,
followers INTEGER NOT NULL,
following INTEGER NOT NULL,
picture VARCHAR (255) NOT NULL
)
CREATE TABLE posts(
id SERIAL PRIMARY KEY,
title VARCHAR (255) NOT NULL,
picture VARCHAR (255) NOT NULL,
description VARCHAR (255) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
likes INTEGER NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
)
CREATE TABLE posts_liked_users(
post_id INTEGER NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
)
CREATE TABLE follows(
following_user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
followed_user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
)
And here is the diagram:
Are the diagram and the overall design right or is there something missing?