Vishal Lohar
Vishal Lohar•4mo ago

Convex Ents Cascading Delete not working

import { defineEnt } from "convex-ents";
import { v } from "convex/values";

export const collectionsSchema = defineEnt({
name: v.string(),
description: v.optional(v.string()),
isPublic: v.boolean(),
updatedBy: v.id("users"),
})
.edge("workspace", {
field: "workspaceId",
})
.edge("user", {
field: "createdBy",
})
.edges("collectionScreens", {
ref: true,
})
.edges("collectionFlows", {
ref: true,
});

export const collectionScreensSchema = defineEnt({
updatedAt: v.union(v.null(), v.null()),
updatedBy: v.id("users"),
platormType: v.string(),
})
.edge("user", {
field: "createdBy",
})
.edge("collection", {
field: "collectionId",
})
.edge("screen", {
field: "screenId",
})
.edge("savedScreen", {
field: "savedScreenId",
});

export const collectionFlowsSchema = defineEnt({
updatedAt: v.union(v.null(), v.number()),
updatedBy: v.id("users"),
platormType: v.string(),
})
.edge("user", {
field: "createdBy",
})
.edge("collection", {
field: "collectionId",
})
.edge("flow", {
field: "flowId",
})
.edge("savedFlow", {
field: "savedFlowId",
});
import { defineEnt } from "convex-ents";
import { v } from "convex/values";

export const collectionsSchema = defineEnt({
name: v.string(),
description: v.optional(v.string()),
isPublic: v.boolean(),
updatedBy: v.id("users"),
})
.edge("workspace", {
field: "workspaceId",
})
.edge("user", {
field: "createdBy",
})
.edges("collectionScreens", {
ref: true,
})
.edges("collectionFlows", {
ref: true,
});

export const collectionScreensSchema = defineEnt({
updatedAt: v.union(v.null(), v.null()),
updatedBy: v.id("users"),
platormType: v.string(),
})
.edge("user", {
field: "createdBy",
})
.edge("collection", {
field: "collectionId",
})
.edge("screen", {
field: "screenId",
})
.edge("savedScreen", {
field: "savedScreenId",
});

export const collectionFlowsSchema = defineEnt({
updatedAt: v.union(v.null(), v.number()),
updatedBy: v.id("users"),
platormType: v.string(),
})
.edge("user", {
field: "createdBy",
})
.edge("collection", {
field: "collectionId",
})
.edge("flow", {
field: "flowId",
})
.edge("savedFlow", {
field: "savedFlowId",
});
20 Replies
Convex Bot
Convex Bot•4mo ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Vishal Lohar
Vishal LoharOP•4mo ago
When i delete collection. THe collection screens doc is not deleted I am new to convex ents. So can anybody help ?
deen
deen•4mo ago
Deleting a collection should delete its collection-screens according to your schema. Make sure that you are deleting the collection through the ents API, and NOT using ctx.db.delete(collectionId) - the standard convex method does not care about your ent's relationships.
Vishal Lohar
Vishal LoharOP•4mo ago
Ohh. My bad. Yes. I was using the normal convex api @deen As you can see in my schema. I have two fields createdBy and updatedBy. I have added one edge for createdBy but cannot add another due to a duplicate edge error. What can I do here? Also, What if I add a field that stores an array of related screen IDs? And one of the screens was deleted. What happens then? The entire doc is deleted or just the deleted screen id is removed from the array
ampp
ampp•4mo ago
https://labs.convex.dev/convex-ents/schema has a section showing this. You can control the name of the field. This becomes necessary if you want to have another field edge between the same pair of tables: - I have not needed to do this yet.
Ent Schema - Convex Ents
Relations, default values, unique fields and more for Convex
ampp
ampp•4mo ago
assuming that array is just v.array(v.ids) and not a edge its not part of the cascade delete.
Vishal Lohar
Vishal LoharOP•4mo ago
@ampp In Supabase, When a related doc is deleted, the id of that doc is removed from the array instead of deleting that doc. Is this possible in convex I know that, If I make an edge it will delete the doc even if its an array. But what If I just want to remove the related doc's id?
ampp
ampp•4mo ago
I tend to just generally avoid storing arrays of data, and would just do it with the a table structure. Yeah i'm not sure, so you are asking for a cascade deleting only its dependents? I mean my quick hack would be just to re-insert the row 🙃 But then you have a new id to deal with. The transactional nature, would make it effectively never "deleted". Oh i read that wrong.. You want to make sure every reference to the id is deleted so it would introspect into arrays within tables?
Vishal Lohar
Vishal LoharOP•4mo ago
Yes If its a array just remove the reference [screen1, screen2]. Screen1 get deleted then remove screen 1 from array
ampp
ampp•4mo ago
It wont delete a whole record because its referenced in a array within a array in a table.
Vishal Lohar
Vishal LoharOP•4mo ago
Okay. So will it remove the reference from the array
ampp
ampp•4mo ago
im fairly sure you just have a dangling reference
Vishal Lohar
Vishal LoharOP•4mo ago
No, It will throw an error if the screen doesn't exist The app is very complex Its a clone of mobbin.com
ampp
ampp•4mo ago
Yeah, but usually you can just do a if statement etc to avoid trying to query something that is gone.
Vishal Lohar
Vishal LoharOP•4mo ago
Yes. I understand. I was just trying to find a way. SInce i already wrote the code and now i am just shifting to ents Anyway, I got the idea how to do this. Thanks
ampp
ampp•4mo ago
Yeah, i would probably have some helper table or row that keeps track of places to cleanup.
Vishal Lohar
Vishal LoharOP•4mo ago
This can work too. I will just replace my getAllOrThrow method and filter null values And use some extra tables like you suggested to schedule the deletion of referenced id
ampp
ampp•4mo ago
It might be a use case for v.records(for that "helper"), i got tons of situations where i hold a tableName and union of Ids in the record that would help. But i also have a huge number of situations where cascade is not possible for me anyway.
Vishal Lohar
Vishal LoharOP•4mo ago
Yes. I can also schedule the query inside the delete screen mutation to remove all the references There will be no need of helper tables
deen
deen•4mo ago
This is what I have ended up doing in the situations where I want to hold a list of ids rather than add tables. ents, and convex in general, doesn't want you to store relationships in arrays because it's technically inefficient. But it can save you a whole lot of complexity as long as you're think carefully about how to manage it yourself. My strategy for id arrays now is always "the document these ids reference might be gone, and that's OK." PS there's a #convex-ents channel if you have more questions

Did you find this page helpful?