- Published
- Author
- Nived HariSystem Analyst
In Prisma, there’s a big difference between directly setting a foreign key and using
• Direct assignment just writes the raw foreign key value — Prisma doesn’t check if the user actually exists.
•
#prisma
connect when creating related records.Code
// ❌ Directly setting the foreign key
users: {
create: {
userId: testUser.id,
},
}
// ✅ Using relation API
users: {
create: {
user: {
connect: { id: testUser.id },
},
},
}• Direct assignment just writes the raw foreign key value — Prisma doesn’t check if the user actually exists.
•
connect uses Prisma’s relation API, validates that the record exists, and safely links them.#prisma