#Id reference from component table
8 messages · Page 1 of 1 (latest)
You can’t use Id<"workflows"> / v.id("workflows") in your app schema or functions when "workflows" is a table defined inside a component. ID validation and Id types don’t cross the component boundary. [Authoring components]
From outside the component:
- All component
Id<"table">types are exposed as plainstringin theComponentApi. [Authoring components] - You also cannot declare
v.id("table")for a table that lives in a different component. [Authoring components]
So in your app code, you need to treat the component’s IDs as strings:
// In your app (outside the component)
import { mutation } from "./_generated/server";
import { v } from "convex/values";
export const deleteByWorkflowId = mutation({
args: {
// workflowId comes from the component, so use string
workflowId: v.string(),
},
handler: async (ctx, args) => {
const onboarding = await ctx.db
.query("onboardings")
.withIndex("by_workflowId", (q) => q.eq("workflowId", args.workflowId))
.unique();
if (onboarding) {
await ctx.db.delete("onboardings", onboarding._id);
}
},
});
And in your schema, store that foreign key as a string as well:
onboardings: defineTable({
// ...
workflowId: v.string(), // references component table
}).index("by_workflowId", ["workflowId"]);
This is the same pattern recommended for Better Auth’s user table: use v.string() for IDs written by a component and look them up via the component’s API. [Better Auth local install; Discord: link to better-auth user]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@zinc geyser Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@fallen garden how can i still manage to get that Id type?
Let me check the knowledge base for you again. One moment! :books:
You can’t safely get a real Id<"workflows"> type in your app code for a table that lives inside a component. At the component boundary, all Id<"table"> types are erased to plain string, and v.id("table") cannot target tables from another component or app. [Authoring components]
So:
- Inside the component: you can and should use
Id<"workflows">andv.id("workflows"). - Outside the component (in your app): you must treat that value as
string(schema fieldv.string(), function argv.string()), even though at runtime it’s an ID string.
There’s no supported way in the current model to “recreate” the component’s Id<"workflows"> type in the parent app; the docs explicitly say those Id types are not useful outside the component and become strings in the ComponentApi. [Authoring components]
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@zinc geyser Kapa.ai is still learning and improving, please let me know how I did by reacting below.