_count doesn't update in many to many relation
See original GitHub issueBug description
I have many-to-many relations in my schema like brand and category. I am using _.count to see how many brands are there in a category. It seems to work but when I delete one of the brand it does not update _.count in the category.
This is the model :
model Category {
id String @id @default(cuid())
name String @unique
createdAt DateTime @default(now())
brands Brand[]
}
model Brand {
id String @id @default(cuid())
name String @unique
createdAt DateTime @default(now())
categories Category[]
}
I am showing how many brands in a category by pulling the data like this
const Data = await prisma.category.findMany({
include: {
_count: {
select: { brands: true }
}
}
});
It works fine. It shows how many brands are in a category. When I try to delete a brand like this;
prisma.brand
.delete({
where: {
id: ID
}
})
The brand gets deleted but the category list doesn’t update. It still includes that brand in _count. I have deleted the same way in one-to-many relation where _count updated. How do I do it in many to many?
How to reproduce
The project is complicated so If you want you can use just two models to follow the bugs
model Category {
id String @id @default(cuid())
name String @unique
createdAt DateTime @default(now())
brands Brand[]
}
model Brand {
id String @id @default(cuid())
name String @unique
createdAt DateTime @default(now())
categories Category[]
}
Expected behavior
When I delete a brand, the Category list (._count) also should be updated. like when I use this code to see.
const Data = await prisma.category.findMany({
include: {
_count: {
select: { brands: true }
}
}
});
Environment & setup
- OS: Windows 10
- Database: MySQL
- Node.js version: v16.14.1
Prisma Version
prisma : 3.11.0
@prisma/client : 3.11.0
Current platform : windows
Query Engine (Node-API) : libquery-engine b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine : migration-engine-cli b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary : prisma-fmt b371888aaf8f51357c7457d836b86d12da91658b (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : b371888aaf8f51357c7457d836b86d12da91658b
Studio : 0.458.0
Preview Features : referentialIntegrity
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (6 by maintainers)
Top GitHub Comments
Yes. The issue has solved!!!
I can confirm this bug with the current version of Prisma with
referentialIntegrity = prisma
. If we usereferentialIntegrity = foreignKeys
, instead, everything seems to be fine. I have target databases MySQL 8.0 and Postgres 14, but the issue doesn’t seem connector-related.Schema
## Test