#Convex Ents Cascading Delete not working

39 messages · Page 1 of 1 (latest)

torpid current
#
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",
  });
late minnowBOT
#

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!

torpid current
#

When i delete collection. THe collection screens doc is not deleted

#

I am new to convex ents. So can anybody help ?

soft anchor
#

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.

torpid current
#

Ohh. My bad. Yes. I was using the normal convex api

torpid current
#

@soft anchor 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

river fractal
river fractal
torpid current
#

@river fractal 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?

river fractal
#

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?

torpid current
#

Yes

#

If its a array just remove the reference

#

[screen1, screen2]. Screen1 get deleted then remove screen 1 from array

river fractal
#

It wont delete a whole record because its referenced in a array within a array in a table.

torpid current
#

Okay. So will it remove the reference from the array

river fractal
#

im fairly sure you just have a dangling reference

torpid current
#

No, It will throw an error if the screen doesn't exist

#

The app is very complex

river fractal
#

Yeah, but usually you can just do a if statement etc to avoid trying to query something that is gone.

torpid current
#

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

river fractal
#

Yeah, i would probably have some helper table or row that keeps track of places to cleanup.

torpid current
#

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

river fractal
#

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.

torpid current
#

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

soft anchor
# torpid current This can work too. I will just replace my getAllOrThrow method and filter null v...

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."