Back

Issue when using Query.equal in an appwrite function using python

  • 2
  • Databases
  • Flutter
  • Functions
  • Cloud
Shivanshu Gupta
13 Mar, 2024, 14:49

Recently I faced an issue while executing the below function to generate a unique referralCode.

`from random import Random from appwrite.services.databases import Databases from appwrite.query import Query

def generateReferralCode(client)->str: databases = Databases(client) docs = { 'total': 1, } referralCode = None while (docs['total'] > 0): referralCode = Random().randint(100000, 999999) docs = databases.list_documents('USER_DATA', 'USERDATA', [ Query.equal('referralCode', referralCode) ]) return referralCode`

It shows me this error: Invalid query: {"method":"equal","attribute":"referralCode","values":["300522"]}

my collection has the following attributes:

name: String referralCode: Integer

This code used to work fine when I wrote it a few days ago, but now it throws an error.

TL;DR
Issue: Error while using `Query.equal` in an appwrite function to generate a unique referralCode. Solution: The issue is with the query syntax. To fix it, update the query to check for the existence of the referralCode as an integer value instead of a string. Change the line in your code: `Query.equal('referralCode', referralCode)` to `Query.equal('referralCode', int(referralCode))`.
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