Skip to content

Instantly share code, notes, and snippets.

@kasir-barati
Last active April 9, 2022 05:53
Show Gist options
  • Save kasir-barati/0bf14714721e600946dc5a2b6135da16 to your computer and use it in GitHub Desktop.
Save kasir-barati/0bf14714721e600946dc5a2b6135da16 to your computer and use it in GitHub Desktop.
relationships in prisma linkedin
// This is your Prisma schema file
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// N:M relationship in prisma
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
generator prismaDbmlGenerator {
provider = "prisma-dbml-generator"
}
model Account {
id String @unique(map: "account_id_constraints") @db.Char(21) // nanoid
userId String @unique(map: "account_user_id_constraints") @map("user_id") @db.VarChar(21)
bio String @db.VarChar(500)
// Relationships
skills AccountSkill[]
User User @relation(fields: [userId], references: [id], map: "account_user_id_fkey")
// TODO:
// feedbackedTo, and responededTo, is many to many self relationship
// followers, and following, is many to many self relationship
// accountFriends, and friendTo is another many to many self relationship
@@id(fields: [id, userId], name: "account_id")
@@map("accounts")
}
model AccountSkill {
experienceLevel String @db.VarChar(20)
yearsOffExperience Int @db.SmallInt
skillId String @map("account_skill_skill_id") @db.Char(21)
accountId String @map("account_skill_account_id") @db.Char(21)
Account Account? @relation(fields: [accountId, accountUserId], references: [id, userId])
accountUserId String? @db.VarChar(21)
Skill Skill @relation(fields: [skillId], references: [id])
@@id(fields: [skillId, accountId])
}
model Skill {
id String @id @db.Char(21) // nanoid
title String @db.VarChar(255)
// Relationships
Account AccountSkill[]
}
// This is your Prisma schema file
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// 1:N relationship
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
generator prismaDbmlGenerator {
provider = "prisma-dbml-generator"
}
model Post {
id Int @id @default(dbgenerated("uuid"))
title String
content String?
published Boolean @default(false)
// https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#now
createdAt DateTime @default(now())
// https://www.prisma.io/docs/reference/api-reference/prisma-schema-reference#updatedat
updatedAt DateTime @updatedAt
// https://www.prisma.io/docs/concepts/components/prisma-client/middleware/soft-delete-middleware
deletedAt DateTime?
// Relationship
// Relation field
User User? @relation(fields: [userId], references: [id])
// Scalar field, Each user hase `many` posts. recall we told that we have to put the scalar field on the many side
// Obviously it is how we do it in SQL world. Puting the FK in a 1:N relationship in N side.
userId String?
@@map("posts")
}
model User {
id String @id @default(dbgenerated("uuid"))
name String
family String
email String @unique(map: "email_unique_constraint")
hashedPassword String
avatar String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
deletedAt DateTime?
// Relationships
// Relation field
posts Post[]
@@map("users")
}
model Comment {
id String @id @default(dbgenerated("uuid"))
User User
}
// This is your Prisma schema file
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// 1:1 relationship in prisma
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
generator prismaDbmlGenerator {
provider = "prisma-dbml-generator"
}
model User {
id String @id @db.Char(21) // nanoid
name String? @db.Char(200)
family String? @db.Char(200)
email String @unique(map: "user_email_unique_constraints") @db.VarChar(320)
username String @unique(map: "user_username_unique_constraints") @db.Char(200)
hashedPassword String @map("hashed_password")
avatar String?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
deletedAt DateTime?
// Relationships
Account Account?
@@map("users")
}
model Account {
id String @unique(map: "account_id_constraints") @db.Char(21) // nanoid
bio String @db.VarChar(500)
// Relationships
userId String @unique(map: "account_user_id_constraints") @map("user_id") @db.VarChar(21)
User User @relation(fields: [userId], references: [id], map: "account_user_id_fkey")
// TODO:
// feedbackedTo, and responededTo, is many to many self relationship
// followers, and following, is many to many self relationship
// accountFriends, and friendTo is another many to many self relationship
@@id(fields: [id, userId], name: "account_id")
@@map("accounts")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment