YoungHappyGuy
YoungHappyGuy
PPrisma
Created by YoungHappyGuy on 7/19/2024 in #help-and-questions
updateMany fails with "You can't specify target table 'Org' for update in FROM clause" error
For the following schema
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}

model Org {
id Int @id @default(autoincrement())
name String
childOrgs Org[] @relation("Relation_Org_ParentChild")
parentOrgs Org[] @relation("Relation_Org_ParentChild")
childOrgsCount Int @default(0)
parentOrgsCount Int @default(0)
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}

model Org {
id Int @id @default(autoincrement())
name String
childOrgs Org[] @relation("Relation_Org_ParentChild")
parentOrgs Org[] @relation("Relation_Org_ParentChild")
childOrgsCount Int @default(0)
parentOrgsCount Int @default(0)
}
running the following query results in an error
await prisma.org.updateMany({
where: {
childOrgs: {
some: {
id: 1,
}
},
},
data: {
childOrgsCount: {
increment: 1,
},
},
})
await prisma.org.updateMany({
where: {
childOrgs: {
some: {
id: 1,
}
},
},
data: {
childOrgsCount: {
increment: 1,
},
},
})
Error:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(MysqlError { code: 1093, message: "You can't specify target table 'Org' for update in FROM clause", state: "HY000" })), transient: false })
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(MysqlError { code: 1093, message: "You can't specify target table 'Org' for update in FROM clause", state: "HY000" })), transient: false })
5 replies