P
Prisma•2w ago
Flinty

Relationship issue

Hey, I'm probably being super duper stupid here, but I am new to Prisma and I'm missing something. I have the schema:
model Ticket {
id BigInt @id
description String @db.LongText
descriptionHtml String @map("description_html") @db.LongText
subject String @db.LongText
displayId Int @map("display_id")
ticketType String? @map("ticket_type")
status Int
priority Int
urgent Boolean
spam Boolean
deleted Boolean
source Int
trained Boolean
createdAt DateTime @map("created_at")
updatedAt DateTime @map("updated_at")
dueBy DateTime? @map("due_by")
frDueBy DateTime? @map("frDueBy")
isEscalated Boolean @map("isescalated")
frEscalated Boolean @map("fr_escalated")
groupId BigInt?
ownerId BigInt?
requesterId BigInt?
responderId BigInt?
accountId Int
slaState Int @default(0)
delta Boolean
dirty Int
customField Json? @map("custom_field")
ccEmail Json? @map("cc_email")
tags Json?
attachments Json?
statusName String? @map("status_name")
requesterStatusName String? @map("requester_status_name")
priorityName String? @map("priority_name")
sourceName String? @map("source_name")
productId BigInt? @map("product_id")
toEmails Json?
reportsData Json? @map("reports_data")
ticketStates Json? @map("ticket_states")

// Relations
requester User? @relation("RequestedBy", fields: [requesterId], references: [id])
responder User? @relation("RespondedBy", fields: [responderId], references: [id])
group Group? @relation(fields: [groupId], references: [id])
owner Company? @relation("OwnedBy", fields: [ownerId], references: [id])

Note Note[]
}
model Ticket {
id BigInt @id
description String @db.LongText
descriptionHtml String @map("description_html") @db.LongText
subject String @db.LongText
displayId Int @map("display_id")
ticketType String? @map("ticket_type")
status Int
priority Int
urgent Boolean
spam Boolean
deleted Boolean
source Int
trained Boolean
createdAt DateTime @map("created_at")
updatedAt DateTime @map("updated_at")
dueBy DateTime? @map("due_by")
frDueBy DateTime? @map("frDueBy")
isEscalated Boolean @map("isescalated")
frEscalated Boolean @map("fr_escalated")
groupId BigInt?
ownerId BigInt?
requesterId BigInt?
responderId BigInt?
accountId Int
slaState Int @default(0)
delta Boolean
dirty Int
customField Json? @map("custom_field")
ccEmail Json? @map("cc_email")
tags Json?
attachments Json?
statusName String? @map("status_name")
requesterStatusName String? @map("requester_status_name")
priorityName String? @map("priority_name")
sourceName String? @map("source_name")
productId BigInt? @map("product_id")
toEmails Json?
reportsData Json? @map("reports_data")
ticketStates Json? @map("ticket_states")

// Relations
requester User? @relation("RequestedBy", fields: [requesterId], references: [id])
responder User? @relation("RespondedBy", fields: [responderId], references: [id])
group Group? @relation(fields: [groupId], references: [id])
owner Company? @relation("OwnedBy", fields: [ownerId], references: [id])

Note Note[]
}
I then have a JS function that reads in a JSON object, and tries to save it to the DB (shortened for brevity):
function toCamelCaseTicket(ticket: any) {
return {
id: BigInt(ticket.id),

requester: ticket.requester_id
? {
connect: { id: BigInt(ticket.requester_id) },
}
: null,

responder: ticket.responder_id
? {
connect: { id: BigInt(ticket.responder_id) },
}
: null,

group: ticket.group_id
? {
connect: { id: BigInt(ticket.group_id) },
}
: null,

owner: ticket.owner_id
? {
connect: { id: BigInt(ticket.owner_id) },
}
: null,
};
}

async function insertTickets(data: any[]) {
for (const entry of data) {
const mapped = toCamelCaseTicket(entry.helpdesk_ticket)
console.log("Mapped", JSON.stringify(mapped, (_, v) => typeof v === 'bigint' ? v.toString() : v));

await prisma.ticket.upsert({
where: { id: BigInt(mapped.id) },
update: {},
create: mapped,
})
}
}
function toCamelCaseTicket(ticket: any) {
return {
id: BigInt(ticket.id),

requester: ticket.requester_id
? {
connect: { id: BigInt(ticket.requester_id) },
}
: null,

responder: ticket.responder_id
? {
connect: { id: BigInt(ticket.responder_id) },
}
: null,

group: ticket.group_id
? {
connect: { id: BigInt(ticket.group_id) },
}
: null,

owner: ticket.owner_id
? {
connect: { id: BigInt(ticket.owner_id) },
}
: null,
};
}

async function insertTickets(data: any[]) {
for (const entry of data) {
const mapped = toCamelCaseTicket(entry.helpdesk_ticket)
console.log("Mapped", JSON.stringify(mapped, (_, v) => typeof v === 'bigint' ? v.toString() : v));

await prisma.ticket.upsert({
where: { id: BigInt(mapped.id) },
update: {},
create: mapped,
})
}
}
I get the repetitive error of:
Unknown argument requester. Did you mean requesterId? Available options are marked with ?.

at An (node_modules/@prisma/client/runtime/library.js:29:1363)
at zn.handleRequestError (node_modules/@prisma/client/runtime/library.js:121:7102)
at zn.handleAndLogRequestError (node_modules/@prisma/client/runtime/library.js:121:6784)
at zn.request (node_modules/@prisma/client/runtime/library.js:121:6491)
at async l (node_modules/@prisma/client/runtime/library.js:130:9778)
at async insertTickets (server/api/import.post.ts:135:1)
at async <anonymous> (server/api/import.post.ts:69:1)
Unknown argument requester. Did you mean requesterId? Available options are marked with ?.

at An (node_modules/@prisma/client/runtime/library.js:29:1363)
at zn.handleRequestError (node_modules/@prisma/client/runtime/library.js:121:7102)
at zn.handleAndLogRequestError (node_modules/@prisma/client/runtime/library.js:121:6784)
at zn.request (node_modules/@prisma/client/runtime/library.js:121:6491)
at async l (node_modules/@prisma/client/runtime/library.js:130:9778)
at async insertTickets (server/api/import.post.ts:135:1)
at async <anonymous> (server/api/import.post.ts:69:1)
2 Replies
Prisma AI Help
Prisma AI Help•2w ago
Skipping coffee for instant knowledge! Let's brew that AI answer now. I can see the issue with your Prisma relationship setup. This is a common problem when working with relations in Prisma. The error you're getting (Unknown argument requester. Did you mean requesterId?) occurs because you're trying to use the relation field name (requester) directly in your create operation, but Prisma expects you to use either the foreign key field (requesterId) or a nested write operation for the relation. Based on the knowledge sources, there are two ways to fix this: 1. Use the foreign key field directly:
function toCamelCaseTicket(ticket: any) {
return {
id: BigInt(ticket.id),
requesterId: ticket.requester_id ? BigInt(ticket.requester_id) : null,
responderId: ticket.responder_id ? BigInt(ticket.responder_id) : null,
groupId: ticket.group_id ? BigInt(ticket.group_id) : null,
ownerId: ticket.owner_id ? BigInt(ticket.owner_id) : null,
// other fields...
};
}
function toCamelCaseTicket(ticket: any) {
return {
id: BigInt(ticket.id),
requesterId: ticket.requester_id ? BigInt(ticket.requester_id) : null,
responderId: ticket.responder_id ? BigInt(ticket.responder_id) : null,
groupId: ticket.group_id ? BigInt(ticket.group_id) : null,
ownerId: ticket.owner_id ? BigInt(ticket.owner_id) : null,
// other fields...
};
}
2. Or keep your nested connect syntax, but make sure to use the correct structure:
function toCamelCaseTicket(ticket: any) {
return {
id: BigInt(ticket.id),
// other fields...

// Use the relation fields correctly
...(ticket.requester_id ? {
requester: {
connect: { id: BigInt(ticket.requester_id) }
}
} : {}),

...(ticket.responder_id ? {
responder: {
connect: { id: BigInt(ticket.responder_id) }
}
} : {}),

...(ticket.group_id ? {
group: {
connect: { id: BigInt(ticket.group_id) }
}
} : {}),

...(ticket.owner_id ? {
owner: {
connect: { id: BigInt(ticket.owner_id) }
}
} : {})
};
}
function toCamelCaseTicket(ticket: any) {
return {
id: BigInt(ticket.id),
// other fields...

// Use the relation fields correctly
...(ticket.requester_id ? {
requester: {
connect: { id: BigInt(ticket.requester_id) }
}
} : {}),

...(ticket.responder_id ? {
responder: {
connect: { id: BigInt(ticket.responder_id) }
}
} : {}),

...(ticket.group_id ? {
group: {
connect: { id: BigInt(ticket.group_id) }
}
} : {}),

...(ticket.owner_id ? {
owner: {
connect: { id: BigInt(ticket.owner_id) }
}
} : {})
};
}
The issue is that when you set requester: null, Prisma doesn't know how to handle this. You should either omit the field entirely when it's null or use the foreign key field directly. Also, be careful with BigInt serialization in your logs. You're already handling this correctly with your custom JSON.stringify replacer function, but it's worth noting that BigInt values can cause serialization errors if not handled properly, as mentioned in this GitHub issue. šŸ¤” Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Nurul
Nurul•2w ago
Hey šŸ‘‹ Can you share the full generated upsert query? What is the value of mapped?

Did you find this page helpful?