Multi-Table delete
Use the with parameter to delete records in one or more related tables as part of the primary deletion operation. This is useful for maintaining referential integrity by cleaning up associated data.
Basic multi-table delete
This example deletes a primary user record and all associated profile and posts records.
- Manta Studio
- PostgreSQL
TypeScript
await manta.deleteRecords({
table: "user",
where: { id: 15 },
with: {
profile: {
on: { from: "id", to: "userId" }, // Maps user.id to profile.userId
},
posts: {
on: { from: "id", to: "authorId" }, // Maps user.id to posts.authorId
},
},
});
TypeScript
await manta.deleteRecords({
db: "production_db",
table: "user",
where: { id: 15 },
with: {
profile: {
on: { from: "id", to: "userId" },
},
posts: {
on: { from: "id", to: "authorId" },
},
},
});
Conditional nested delete
Use the when clause inside the nested operation to apply the related delete only if the target linked record meets a specific condition.
- Manta Studio
- PostgreSQL
TypeScript
await manta.deleteRecords({
table: "user",
where: { status: "inactive" },
with: {
profile: {
on: { from: "id", to: "userId" },
when: { verified: { equals: false } }, // Delete profile ONLY if verified is false
},
},
});
TypeScript
await manta.deleteRecords({
db: "production_db",
table: "user",
where: { status: "inactive" },
with: {
profile: {
on: { from: "id", to: "userId" },
when: { verified: { equals: false } },
},
},
});