Home > database >  Create database structure with prisma (postgresql)
Create database structure with prisma (postgresql)

Time:09-13

I have to setup a database with prisma module for a project.

I want to have a structure like this:

Person {
  lastname: string
  firstname: string
  age: number
  bio: string
  photos: Photo[]
}

Photo {
  id: string,
  x: number,
  y: number
}

How to translate this to prisma file?

CodePudding user response:

This is what the corresponding Prisma schema would look like:

model Person {
  id        String  @id @default(uuid())
  lastname  String
  firstname String
  age       Int
  bio       String
  photos    Photo[]
}

model Photo {
  id       String @id @default(uuid())
  x        Int
  y        Int
  personId String
  person   Person @relation(fields: [personId], references: [id])
}

Note that this is the minimum viable schema for the model that you've described. The id fields (with an @id attribute) are required on every Prisma model. Similarly, the personId and person fields on Photo are required in Prisma because every relation between models needs to be declared on both sides.

You can read more about data modeling in the Prisma docs.

  • Related