Prisma relations

Prisma relations solve a huge problem with databases and data handling.

Suppose you have a list of users in your app, that create tweets (imagine Twitter).

In your schema you can define the relation between those 2 entities in this way:

model Tweet {
  id Int @id @default(autoincrement()) 
  text String
  author User @relation(fields: [authorId], references: [id])
  authorId Int
}

model User {
  id Int @default(autoincrement()) @id
  tweets Tweet[]
}

When you create a new tweet you associate it with a user with id 1 in this way:

await prisma.tweet.create({
  data: {
    text: req.body.content,
    author: {
      connect: { id: 1 }
    }
  }
})

Then you can retrieve the author information when you get one tweet, with:

await prisma.tweet.findMany({
  include: {
    author: true
  }
})

You can also create a user and populate the database with 2 tweets associated to it:

await prisma.user.create({
  data: {
    tweets: {
      create: [
        { text: 'test' },
        { text: 'test2' },
      ]
    }
  }
})


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Prisma relations solve a huge problem with databases and data handling.

Suppose you have a list of users in your app, that create tweets (imagine Twitter).

In your schema you can define the relation between those 2 entities in this way:

model Tweet {
  id Int @id @default(autoincrement()) 
  text String
  author User @relation(fields: [authorId], references: [id])
  authorId Int
}

model User {
  id Int @default(autoincrement()) @id
  tweets Tweet[]
}

When you create a new tweet you associate it with a user with id 1 in this way:

await prisma.tweet.create({
  data: {
    text: req.body.content,
    author: {
      connect: { id: 1 }
    }
  }
})

Then you can retrieve the author information when you get one tweet, with:

await prisma.tweet.findMany({
  include: {
    author: true
  }
})

You can also create a user and populate the database with 2 tweets associated to it:

await prisma.user.create({
  data: {
    tweets: {
      create: [
        { text: 'test' },
        { text: 'test2' },
      ]
    }
  }
})


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-07-13T05:00:00+00:00) Prisma relations. Retrieved from https://www.scien.cx/2021/07/13/prisma-relations/

MLA
" » Prisma relations." flaviocopes.com | Sciencx - Tuesday July 13, 2021, https://www.scien.cx/2021/07/13/prisma-relations/
HARVARD
flaviocopes.com | Sciencx Tuesday July 13, 2021 » Prisma relations., viewed ,<https://www.scien.cx/2021/07/13/prisma-relations/>
VANCOUVER
flaviocopes.com | Sciencx - » Prisma relations. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/13/prisma-relations/
CHICAGO
" » Prisma relations." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/07/13/prisma-relations/
IEEE
" » Prisma relations." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/07/13/prisma-relations/. [Accessed: ]
rf:citation
» Prisma relations | flaviocopes.com | Sciencx | https://www.scien.cx/2021/07/13/prisma-relations/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.