In the previous step, we added attributes (columns) to all our entities (tables).

Now we’ll connect them together using relationships.

πŸ‘‰ A relationship shows how two entities are linked to each other.

For example:

In Prisma (or any relational database), we connect tables using Foreign Keys (FKs) and the @relation() field.


πŸ”— 1. Users ↔ Profile (One-to-One)

model User {
  id          String     @id @default(uuid())
  username    String     @unique
  password    String
  email       String     @unique
  isVerified  Boolean    @default(false)
  profile     Profile?   @relation("UserProfile")
  tweets      Tweet[]
  notifications Notifications[]
  messagesSent     Messages[]   @relation("SentMessages")
  messagesReceived Messages[]   @relation("ReceivedMessages")
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
}

model Profile {
  id          String          @id @default(uuid())
  bio         String?
  profileCategory ProfileCategory
  userId      String          @unique
  user        User            @relation("UserProfile", fields: [userId], references: [id], onDelete: Cascade)
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

Understand The One to One Relationship

First, understand what’s happening logically


🐦 2. Users ↔ Tweets (One-to-Many)