这个社交媒体平台 PostgreSQL 数据库设计正确吗?

Is this Social Media Platform PostgreSQL Database design right?

这是一个简单的社交媒体平台的持久化设计。目前,有这些 tables:

这是创建 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
  )

这是图表:

图表和整体设计是否正确或是否缺少某些内容?

作为对任何遇到类似问题的人的回答,我根据一些建议和研究重构了设计:

  • 我将 VARCHAR 字段更新为 TEXT,这是一般指南。

  • 因为normalization I removed the followers and following from the users and likes from the post in order to reduce data redundancy and improve data integrity.

  • 我在 followsposts_liked_users 上添加了一个 created_at 字段,以记录用户关注另一个人或喜欢 post 的时间。

CREATE TABLE users(
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  username TEXT UNIQUE NOT NULL,
  password TEXT NOT NULL,
  email TEXT NOT NULL,
  bio TEXT NOT NULL,
  picture TEXT NOT NULL
)

CREATE TABLE posts(
  id SERIAL PRIMARY KEY,
  title TEXT NOT NULL,
  picture TEXT NOT NULL,
  description TEXT NOT NULL,
  content TEXT NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
)

CREATE TABLE posts_liked_users(
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  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(
  created_at TIMESTAMP NOT NULL DEFAULT NOW(),
  following_user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  followed_user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE
)

参考文献: