I am making a gym website for a project and ran into trouble with ordering a sqlalchemy field in descending order.
I am trying to give each user a unique barcode. I generate a random barcode and then check if it is already in use. If it is, another barcode is generated until one is made that isn't in use.
This is my code:
barcode = save_barcode(barcode_generator())
barcode_list = []
last_id = User.query.order_by(desc(id))
for x in range(1,last_id+1):
temp_barcode = User.query.filter_by(id=x).first()
barcode_list.append(temp_barcode.barcode)
while barcode in barcode_list:
barcode = save_barcode(barcode_generator())
The save_barcode and barcode_generator functions are ones I have made earlier in the program. A random barcode is generated fine using them (I checked in DB Browser). However, as soon as I added the routine to check if the barcode is already in use, I get an error about my order_by statement.
This is the error:
sqlalchemy.exc.ArgumentError: GROUP BY / OF / etc. expression expected, got <built-in function id>
Any help would be appreciated.