Back

error: 'Document with the requested ID already exists.' even when using ID.unique()

  • 0
  • Databases
  • Cloud
Booyah Squadians
30 Mar, 2025, 15:15

PS: tried to create docs without the relational stuff and the issue still persists..

TL;DR
Developers are facing an error "Document with the requested ID already exists." despite using ID.unique(). They tried adding unique indexes, but the issue persists. They're experiencing the problem specifically with a core collection when creating multiple documents at once. The code snippet provided showcases the attempt to create documents with relational data, and the issue persists in the core collection. Initially, the user thought it was a relational problem, but even without relations, the error occurs. Other collections work fine. It seems to be a unique problem related to the core collection.
Booyah Squadians
30 Mar, 2025, 15:16

I am trying to create multiple docs at the same time Like one by one going over the inputs of the users

Booyah Squadians
30 Mar, 2025, 15:17

something like :

TypeScript
for coreData in userUploads{
  const coreDoc = await databases.createDocument(
    DATABASE_ID,
    CORE_COLLECTION_ID,
    ID.unique(),  
    coreData
  );
}
Booyah Squadians
30 Mar, 2025, 15:17

and idk why same loop is working for all other collections but when I try to upload in this specific core collection

Booyah Squadians
30 Mar, 2025, 15:18

then only the 1st doc gets generated while all the rest get issues of "document with requested ID already exists"

Booyah Squadians
30 Mar, 2025, 15:19

and the doc that actually exists in the db is with some completely random ID like : 67e95fe700060d076680 and if i clear the db and run again I again get completely unique id like 67e960dc000b6b5b40a2

Booyah Squadians
30 Mar, 2025, 15:32
TypeScript
import { Databases, ID, Query } from 'node-appwrite'; 
const DATABASE_ID = "sample_db";
const CORE_COLLECTION = "users_core";
const SOCIAL_COLLECTION = "users_social";
const PREFS_COLLECTION = "users_preferences";

// Sample data structure
const sampleUser = {
  name: "Alex Smith", 
  // Social links
  social: {
    github: "github.com/alex", 
  },
  // Preferences
  preferences: { 
    language: "en"
  }
};

async function createUserWithRelations() {
  try {
    const userUUID = ID.unique();
    
    // Create social links document
    const socialDoc = await databases.createDocument(
      DATABASE_ID,
      SOCIAL_COLLECTION,
      ID.unique(),
      {
        userUUID: userUUID,
        github: sampleUser.social.github,
      }
    );
    
    // Create preferences document
    const prefsDoc = await databases.createDocument(
      DATABASE_ID,
      PREFS_COLLECTION,
      ID.unique(),
      {
        userUUID: userUUID, 
        language: sampleUser.preferences.language
      }
    );
    
    // Create core user document with references
    const coreDoc = await databases.createDocument(  // <--- Error here 
      DATABASE_ID,
      CORE_COLLECTION,
      ID.unique(),
      {
        userUUID: userUUID,
        name: sampleUser.name, 
        socialId: socialDoc.$id,
        preferencesId: prefsDoc.$id
      }
    );  
    
    return {
      core: coreDoc,
      social: socialDoc,
      preferences: prefsDoc
    };
  } catch (error) {
    console.error("Error creating user:", error);
    throw error;
  }
}
 
Booyah Squadians
30 Mar, 2025, 15:32

this is a pseudo code of what I'm trying to do /how im working with my relational db

Booyah Squadians
30 Mar, 2025, 15:34

also tried the nested method , something like this :

TypeScript

async function createDocument() {
  try {
    // Single document creation with nested data
    const doc = await databases.createDocument(
      DATABASE_ID,
      MAIN_COLLECTION,
      ID.unique(),
      {
        name: sampleData.name,
        email: sampleData.email,
        // Directly embedding related data as nested objects
        socialMedia: sampleData.social,
        preferences: sampleData.preferences
      }
    );
    
    return doc;
  } catch (error) {
    console.error("Error creating document:", error);
    throw error;
  }
}

and even this is not working

Booyah Squadians
30 Mar, 2025, 15:35

response: '{"message":"Document with the requested ID already exists. Try again with a different ID or use ID.unique() to generate a unique ID.","code":409,"type":"document_already_exists","version":"1.6.2"}'

Steven
30 Mar, 2025, 15:36

Do you have a unique index on any of those attributes?

Booyah Squadians
30 Mar, 2025, 15:42

initially I was not having a unique index as I thought the appwrite $id does a unique index automatically but now I just added a unique index as the UUID of the user and still the issue persists

Steven
30 Mar, 2025, 15:45

So you didn't have any unique indexes before?

Does this collection have any relationship attributes? do you have unique indexes on any of your collections?

Steven
30 Mar, 2025, 15:47

Btw, relationship attributes are experimental and should be used with caution as they can lead to performance problems

Booyah Squadians
30 Mar, 2025, 15:50

yes

Booyah Squadians
30 Mar, 2025, 15:51

I have a unique Index called user UUID in all of the 3 collections

Steven
30 Mar, 2025, 15:51

There were two questions

Booyah Squadians
30 Mar, 2025, 15:51

just a sec answering all

Booyah Squadians
30 Mar, 2025, 15:53
  1. yes , I didnt have any unique indexes before i did tried to create indexes when setting up the db but at that time the cloud was having the issue with the databases so I was unable to create the unique indexes at that time
Steven
30 Mar, 2025, 15:53

Perhaps a new document is trying to be created instead of linking the existing document which is why you see the duplicate error

Steven
30 Mar, 2025, 15:55

Can you share your exact code?

Booyah Squadians
30 Mar, 2025, 15:59
  1. about the unique indexes in collections I have a unique index of UUID in each of the collection

also really sorry for my dumbness , its my first time working with appwrite db 😅

Booyah Squadians
30 Mar, 2025, 15:59

thats honestly what I also thought but then I tried to use nexted way of creating the document and still facing the same issue

Booyah Squadians
30 Mar, 2025, 16:00

can I just send it over on dms?

Booyah Squadians
30 Mar, 2025, 16:00

its almost 80% identical , only the variable names are different

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more