Getting appwrite.exception.AppwriteException: Invalid document structure: for even for valid request
- 0
- Databases

I am trying to create document
requestBody = {
"districtName": row['districtName'],
"pincode": row['pincode'],
}
print(f"requestBody--> {requestBody}")
result = databases.create_document(
databaseId,
collectionId,
ID.unique(),
requestBody
)
and request which is printed in the terminal is
requestBody--> {'districtName': 'SOUTH EAST DELHI', 'pincode': 110003}
I don't see any issue with request body then why this error? Can anyone pls help point if I am doing something dumb things or have missed something ?

What's the full error and what's your collection?

Traceback (most recent call last):
File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\client.py", line 97, in call
response.raise_for_status()
File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.bitecope.com/v1/databases/6451d90b58e5630094e5/collections/64535aec379151d13abf/documents
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\MVP\appwrite\dump_pincode_in_appwrite.py", line 30, in <module>
result = databases.create_document(
File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\services\databases.py", line 967, in create_document
return self.client.call('post', path, {
File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-p File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\services\databases.py", line 967, in create_document return self.client.call('post', path, {
File "C:\Users\harsh\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\appwrite\client.py", line 109, in call
raise AppwriteException(response.json()['message'], response.status_code, response.json().get('type'), response.json())
appwrite.exception.AppwriteException: Invalid document structure: Attribute "pincode" has invalid format. Value must be a valid range between 6 and 6

this is kinda full traceback

Value must be a valid range between 6 and 6
That's the problem...

pincode should be a number between 6 and 6. it doesn't mean the number of digits

yaa, I don't know why it's happening
requestBody--> {'districtName': 'SOUTH EAST DELHI', 'pincode': 110003}
here in print it does have valid

no that is not valid

oh wait does it mean 6 like literal 6 ? because I thought 6 mean.. len 👀

yes, literal 6

😆 oh no

such a silly thing I thought it works like len that being counted

I guess will re-try and confim here

is it possible to do insert all?

just saw that it's not possible at this moment in time https://github.com/appwrite/appwrite/issues/3051

for now I did try creating concurrently... Incase anyone want to know the code or for me for future refrence...

from appwrite.client import Client
from appwrite.services.databases import Databases
from appwrite.id import ID
import json
import concurrent.futures
endpoint = '<ENDPOINT>'
# Replace <PROJECT_ID> with your Appwrite project ID
project_id = '<PROJECT_ID>'
# Replace <API_KEY> with your Appwrite API key
api_key = '<API_KEY>'
# Set the <DATABASE_ID> for the Appwrite database
database_id = '<DATABASE_ID>'
# Set the <COLLECTION_ID> for the Appwrite documents
collection_id = '<COLLECTION_ID>'
# Initialize the Appwrite client
client = Client().set_endpoint(endpoint).set_project(project_id).set_key(api_key)
databases = Databases(client)
# Create an Appwrite client and database object
client = Client()
client.set_endpoint(endpoint)
client.set_project(project_id)
client.set_key(api_key)
database = Databases(client)
# Define a function to create a single document in Appwrite
def create_document(document):
result = database.create_document(database_id, collection_id, ID.unique(), document)
return result['$id']
# Load the JSON data from the input file
with open('pincode_unique.json', 'r') as input_file:
json_data = json.load(input_file)
# Create a thread pool to create the documents concurrently
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
# Submit each dictionary in the JSON data to the thread pool
futures = [executor.submit(create_document, document) for document in json_data]
# Print the results of each completed future
for future in concurrent.futures.as_completed(futures):
print(future.result())

created 18 thoushand document in couple of mins

I mean 18K 😅

Fun-fact I didn't wrote this code not even a single line, I asked https://codeium.com/
It's free VS-code extension if incase anyone what to use 👀 quite facinating it would have taken be hours to fig this out

can i get the same code in flutter as not familiar with js

yes, sure... it's one line switch

I assume you do have toJson function already...

no i dont have, i was jsut thinking about this json fnction that how i can make

Future<List<T>> executeConcurrently<T>(List<Future<T>> futures) async {
final results = <T>[];
for (final future in futures) {
try {
final result = await future;
results.add(result);
} catch (e) {
print('Error executing future: $e');
}
}
return results;
}
Recommended threads
- Invalid relationship value. Must be eith...
I'm getting this error when i try to update a document. The collection has a relatipship with 2 other collections. I don't understand why, when i update the doc...
- How do I do this. Please help!
Hey guys! Okay AI can't help so I'm asking real people now. 😓 Anyway I wanted to know how one solve this in Appwrite right now. So I have a form where I wante...
- Delete account on authentication
I'm building an authentication that need to a user a choice of delete their account when they need and on the documentation, if I find delete sessions or sessio...
