Gorka Cesium
Gorka Cesium11mo ago

how to delete the references in other tables to a deleted item?

If i delete a quoteItem, how can i delete also the other tables that are referencing this item ?
export const delete_ = mutation({
args: {
id: v.id('quoteItems'),
},
handler: async ({ db }, { id }) => {
// Validate arguments here. Throw error if invalid. Example `typeof city !== "string" ||`
if (typeof id !== 'string') {
throw new Error('Invalid argument')
}
// TODO how to clean other tables that reference this item?
return await db.delete(id)
},
})
export const delete_ = mutation({
args: {
id: v.id('quoteItems'),
},
handler: async ({ db }, { id }) => {
// Validate arguments here. Throw error if invalid. Example `typeof city !== "string" ||`
if (typeof id !== 'string') {
throw new Error('Invalid argument')
}
// TODO how to clean other tables that reference this item?
return await db.delete(id)
},
})
2 Replies
Michal Srb
Michal Srb11mo ago
db.query for the documents that need deleting, then you can loop over them with db.delete This way you can be very explicit about how the deletion "cascades" through your database. For a more automated approach you could use Ents: https://labs.convex.dev/convex-ents/schema/deletes
Cascading Deletes - Convex Ents
Relations, default values, unique fields and more for Convex
Gorka Cesium
Gorka CesiumOP11mo ago
so i would have to add an index to the other tables to find the ones referencing to the deleted item to be able to delete those references? Ents would be ideal but i'm migrating two projects that are already in production and i dont have the bandwidth to use a beta library in these projects

Did you find this page helpful?