Back

how to make a attribute unique and it should be optional so it can store null or empty string

  • 0
  • Flutter
conqueror
2 Jul, 2023, 12:51

its giving error while leaving the optional field as empty because field is already empty string that is why its giving error. AppwriteException: document_already_exists, Document with the requested ID already exists. (409)

TL;DR
The user is trying to make an attribute unique and optional, allowing it to store null or an empty string. They are facing an issue where an empty string is being stored, but it is not visible in the appwrite console. They have shared their code and are not sure why it is not working. They have confirmed that they are passing null as the value for the attribute. Another user suggests checking if the document ID already exists and provides a link with more information about unique attributes. There is no solution provided in the thread.
conqueror
2 Jul, 2023, 12:51

any one kows the solution ?

Nevus
2 Jul, 2023, 13:16

Are you manually setting the document ID? Are you using a SDK?

Nevus
2 Jul, 2023, 13:19

For the unique attribute take a look at: https://github.com/appwrite/appwrite/discussions/1492

conqueror
2 Jul, 2023, 15:29

both

Nevus
2 Jul, 2023, 15:30

Can you share the code?

Nevus
2 Jul, 2023, 15:36

You can use the console to check if there's already a document with the id you are passing.

Nevus
2 Jul, 2023, 15:57

Also note that if you mark an attribute as unique and try to create two documents with same value for unique attribute the error message is the one you mentioned AppwriteException: document_already_exists, Document with the requested ID already exists. (409) with no indication about which attribute is causing the error.

conqueror
2 Jul, 2023, 16:46

yes i know about this

Drake
2 Jul, 2023, 18:04

Are you making sure to pass null rather than empty string?

conqueror
23 Jul, 2023, 17:12

yes sending null

conqueror
23 Jul, 2023, 17:12

yes sending null and also tried empty string but its not working for unique attribute

Drake
23 Jul, 2023, 17:25

What's your code? Can you do a get collection on the collection and share the output?

Drake
23 Jul, 2023, 17:28

it definitely should work. In the screenshot below i have a unique index on s and the first two documents have s set to null

conqueror
23 Jul, 2023, 17:50

not using console ,

here is my code

TypeScript
            databaseId: databaseId,
            collectionId: productDataCollectionId,
            documentId: documenId,
            data: {
              "sellingPrice": price,
              "totalQuantity": quantity,
              "productName": pname,
              "image": imageUrl,
              "itemCode":null,
            });```
Drake
23 Jul, 2023, 18:19

It shouldn't matter whether it's from the console or not since it's the same API.

Please share your collection as JSON

conqueror
24 Jul, 2023, 18:24

Now its working,

conqueror
24 Jul, 2023, 18:25

but why its storing empty string if sending data like itemCode:"" , and this empty string is not showing in appwrite console ,

conqueror
24 Jul, 2023, 18:29

due to this empty string i am getting document with request id is alredy exist.

Drake
24 Jul, 2023, 18:34

what do you mean?

Drake
24 Jul, 2023, 18:34

please share your collection as JSON

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