Simple relationship on Prisma - prisma

i hope you're all good!
My question is the following:
I have the current models:
model transaction {
id Int #id #default(autoincrement())
user String #db.VarChar(45)
}
model users_temp {
id Int #id #default(autoincrement())
name String #db.VarChar(45)
}
I want to achieve that the user field of transaction model, be one of users_temp.
Example:
transaction1: id: 1, user: testUser1
transaction2: id: 2, user: testUser1
transaction3: id: 3, user: testUser1
transaction4: id: 4, user: testUser2
what should i send from the frontend and how can i structure the models?
Thanks!

Related

Unique constraint failed on the constraint: `Bug_authorId_key`

I am trying to implement a relation querie with Prisma ORM but this error does not allow.
My system consists in a bugtracker, where the user can report bugs. So I'm linking the User with the bugs. When I try to create a new data this error appears: "Unique constraint failed on the constraint: Bug_authorId_key"
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model User {
id Int #id #default(autoincrement())
name String
email String #unique
password String
role String #default("User")
bugs Bug[]
teamId Int?
team Team? #relation(fields: [teamId], references: [id])
}
model Team {
id Int #id #default(autoincrement())
name String
users User[]
admins Admin[]
}
model Admin {
id Int #id #default(autoincrement())
name String
email String #unique
password String
role String #default("Admin")
teamId Int?
team Team? #relation(fields: [teamId], references: [id])
}
model Bug {
id Int #id #default(autoincrement())
title String
description String
status String
authorId Int
author User #relation(fields: [authorId], references: [id])
}
const bug = await prisma.bug.create({
data: {
title: req.body.title,
status: req.body.status,
description: req.body.description,
author: {
connect: {id: req.body.authorId}
}
}
})

Does Prisma create a new relationship collection when using MongoDB?

Looking at the Prisma docs and they show the example below. In this instance, if I'm using MongoDB and define my schema as per below, does Prisma create a collection for me called _CategoryToPost on my behalf to record the many to many relationships?
Or do I have to create my own collection to document this relationship?
model User {
id Int #id #default(autoincrement())
email String #unique
name String?
role Role #default(USER)
posts Post[]
profile Profile?
}
model Profile {
id Int #id #default(autoincrement())
bio String
user User #relation(fields: [userId], references: [id])
userId Int #unique
}
model Post {
id Int #id #default(autoincrement())
createdAt DateTime #default(now())
title String
published Boolean #default(false)
author User #relation(fields: [authorId], references: [id])
authorId Int
categories Category[] #relation(references: [id])
}
model Category {
id Int #id #default(autoincrement())
name String
posts Post[] #relation(references: [id])
}
enum Role {
USER
ADMIN
}
Link to docs
Prisma will create the necessary collections when you run the npx prisma db push command. You should not need to worry about the underlying structure beyond that.

Prisma One-to-one relation issue

I have just recently started using prisma and I ran into an issue with relations. I have a user model and an address model.
model User {
id Int #id #unique #default(autoincrement())
username String
}
model Address {
id Int #id #unique #default(autoincrement())
street String
}
I need to add 2 addresses to the user: invoicing address and delivery address. In the address I don't need a user reference.
I thought this would work without issues by adding this to the user model:
invoiceAddress Address? #relation(name: "iAddress", fields: [iAddressId], references: [id])
deliveryAddress Address? #relation(name: "dAddress", fields: [dAddressId], references: [id])
iAddressId Int?
dAddressId Int?
But when saving the schema two user fields are added to the address model... which I don't need and now I have issues because they reference the same user model so I have to also name them and add scalar field...
Am I missing something??? This should be a basic use case imo.
This is a requirement from Prisma's end that relation fields should exist on both sides of the model, you cannot define relation field on only one model.
The following schema model should solve the issue for you:
model User {
id Int #id #unique #default(autoincrement())
username String
invoiceAddress Address? #relation(name: "iAddress", fields: [iAddressId], references: [id])
deliveryAddress Address? #relation(name: "dAddress", fields: [dAddressId], references: [id])
iAddressId Int?
dAddressId Int?
}
model Address {
id Int #id #unique #default(autoincrement())
street String
UserInvoice User[] #relation(name: "iAddress")
UserDelivery User[] #relation(name: "dAddress")
}
Please note that relation fields do not exist on database so UserInvoice and UserDelivery columns would not exist on Address table. Similarly invoiceAddress and deliveryAddress columns would not exist on User table.

How to select a custom User model property?

I added a company property to the User model in my prisma.schema file (The rest of the prisma.schema file is still similar to the one in the documentation: https://next-auth.js.org/adapters/prisma)
model User {
id String #id #default(cuid())
name String?
email String? #unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
company Company?
}
model Company {
id Int #id #default(autoincrement())
companyName String #unique
gender String
firstName String
lastName String
street String
houseNumber Int
postcode Int
city String
country String
countryCode String
callNumber Int
emailAddress String
website String?
socials Json?
companyUser User #relation(fields: [companyUserId], references: [id])
companyUserId String #unique
}
The whole authentification process is working fine even after the change but when I try to select a User from the database it only returns a certain portion of the User namely the id, name, email, emailVerified and image property.
How can I change this behaviour?
const user = await prisma.user.findUnique({
where: {
id: ...
}
})
For sure I could only create the Company model without connecting it to the User model and maybe adding the User's id to it to have an implicit connection, but that's undermining the whole purpose...
you're looking for nested reads, if you want to include the whole company model you should use include with the name of the relation, note that this will return all the fields for that specific relation:
const user = await prisma.user.findUnique({
where: {
id: ...
},
include: {
company: true,
},
})
if you want to return specific relation fields with the whole user you should use select inside include:
const user = await prisma.user.findUnique({
where: {
id: ...
},
include: {
company: {
select : {
firstName: true,
},
},
},
})

How to filter for related objects in Prisma?

How can query relationships in Prisma? I have the following schema:
model User {
id Int #id #default(autoincrement())
name String?
profile Profile
}
model Profile {
id Int #id #default(autoincrement())
user User #relation(fields: [userId], references: [id])
userId Int
}
How can I query a Profile for a specific User?
Found the answer myself:
prisma.user.findUnique({ where: { id: 42 }}).profile()