Gamius
Gamius•4mo ago

unique() query returned more than one result

Hello Support, Ive a question about our database. I get the error: "Uncaught Error: unique() query returned more than one result" if I have more than one document with an empty string. I know its unique but I also cant set more than one to unset. What should I do if I dont want to safe a value and want to make the value unset?
16 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!
lee
lee•4mo ago
can you share more code and more about what you're trying to do? in Convex the .unique() method is not a property of the field, it's a property of a query. If you want to query for documents with field=unset, and it's possible there is more than one of those, you can use .collect() or .first()
Gamius
GamiusOP•4mo ago
Yeah sure. Thats the Schema
No description
Gamius
GamiusOP•4mo ago
const user = ctx.table("users").getX("clerkId", identity.tokenIdentifier); const updates: { email?: string; lastName?: string; firstName?: string } = {}; if (args.data.email) { updates.email = args.data.email; } if (args.data.lastName) { updates.lastName = args.data.lastName; } if (args.data.firstName) { updates.firstName = args.data.firstName; } // Use one patch instead of a few singular patches for better performance try { await user.patch(updates); await ctx.scheduler.runAfter(0, internal.users.updateUserDataClerk, { data: args.data, identity: identity.tokenIdentifier, }); } catch (e) { console.error(e); if (e instanceof Error && e.message.includes("email")) { throw new Error("Email already in use"); thats the way I patch the data in the db
lee
lee•4mo ago
oh you're using Ents I think it's a reasonable feature request that Ents unique fields drop the uniqueness property (maybe based on a flag?) when it comes to unset/undefined. But as far as I'm aware, that's not an existing feature
Gamius
GamiusOP•4mo ago
Ah okay Thank you. I have to solve the problem like a other way
lee
lee•4mo ago
please submit feature request here 🙂 https://github.com/get-convex/convex-ents/issues
GitHub
Issues · get-convex/convex-ents
Contribute to get-convex/convex-ents development by creating an account on GitHub.
wess
wess•4mo ago
speaking of which, what is the way of enforcing a field to be unique? is there a way to define it on the schema level?
ampp
ampp•4mo ago
Just with convex-ents otherwise you have to do it all manually. Generally you should design your code so there is only one insert command per table so you can enforce these things. Yeah uniqueness failing on null or undefined has got me a couple times from what i recall. Recently i had a promise.all insert that was inserting the same data twice bypassing the checks. Then that would cause a error on read as i was expecting one row but not using .first()
ibrahimyaacob
ibrahimyaacob•4w ago
@lee i like to re-open this ticket, im using convex ents and my query is just using .getX(id) and somewhat im getting this error as well
ibrahimyaacob
ibrahimyaacob•4w ago
No description
ibrahimyaacob
ibrahimyaacob•4w ago
No description
ibrahimyaacob
ibrahimyaacob•4w ago
doesnt make sense to me that query by id returns this error
erquhart
erquhart•4w ago
@ibrahimyaacob this post (the one you're commenting on) is really a feature request, I'd open a separate post with the issue you're running into
ibrahimyaacob
ibrahimyaacob•4w ago
oh i didnt read

Did you find this page helpful?