When using a `connect` or `create` block in a create or update, other scalar relation fields cannot be used directly
See original GitHub issueBug description
I have a model with multiple relations to single entities. When I try to .create
with this model, I have the option of providing those relations via passing the IDs of the foreign entities to the _id
columns, or I can use Prisma’s relations to perform a connect: {}
.
However, when I want to perform a nested write, creating one of the related entities when I create the main entity, I am not able to pass the ID to create the other relation - prisma requires me to use the connect: {}
method instead.
This feels like a bug.
Apologies if this is already an Issue, I’m not really sure how to articulate a search for this.
How to reproduce
Given this schema:
model MainItem {
id Int @id @default(autoincrement())
related_item_one_id Int
related_item_one RelatedItemOne @relation("main_item__to__related_item_one", fields: [related_item_one_id], references: [id])
related_item_two_id Int
related_item_two RelatedItemTwo @relation("main_item__to__related_item_two", fields: [related_item_two_id], references: [id])
}
model RelatedItemOne {
id Int @id @default(autoincrement())
api_keys MainItem[] @relation("main_item__to__related_item_one")
}
model RelatedItemTwo {
id Int @id @default(autoincrement())
api_keys MainItem[] @relation("main_item__to__related_item_two")
}
This works fine:
prisma.mainItem.create({
data: {
related_item_one_id: 123,
related_item_two_id: 456,
}
})
However, this throws a TypeError:
prisma.mainItem.create({
data: {
related_item_one_id: 123,
related_item_two: {
connect: {
id: 456
}
},
}
})
// Types of property 'related_item_one_id' are incompatible.
// Type 'number' is not assignable to type 'undefined'. ts(2322)
That example is somewhat less useful though than my current use case: performing a nested write to create the related entity. This works fine:
prisma.mainItem.create({
data: {
related_item_one: {
connect: {
id: 123
}
},
related_item_two: {
create: {}
},
}
})
However, this throws the TypeError:
prisma.mainItem.create({
data: {
related_item_one_id: 123,
related_item_two: {
create: {}
},
}
})
// Types of property 'related_item_one_id' are incompatible.
// Type 'number' is not assignable to type 'undefined'. ts(2322)
Expected behavior
I would expect these more terse versions to be valid.
Prisma information
(see above)
Environment & setup
- OS: Mac OS
- Database: PostgreSQL
- Node.js version: “12.20.1”
- Prisma version: “2.21.2”
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:11 (3 by maintainers)
Top GitHub Comments
@Karl-EdwardFPJeanMehu Thanks for the reply!
Putting nested writes to the side for a moment, and merely creating a record with already-existing foreign keys, both of these are valid:
Yet, this version that includes one relation via the
connect {}
block, and one through directly passing the id, is invalid.What’s the functional difference between these 3 versions? Shouldn’t all 3 be valid?
Let’s update the issue title then 😄