0

i am trying to return a value from db and getting this error.I tried the previously answered questions here,but no luck.Can anyone help me?

@frappe.whitelist()
def generate_barcode():

    last_barcode = frappe.db.sql("""\
        select MAX(barcode) from `tabItem` """)

    if last_barcode:
        last_barcode = last_barcode + 1

    else:
        x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
        random.shuffle(x)
        last_barcode = x[0]



    return {'last_barcode':last_barcode}

adding traceback:

Traceback (innermost last):
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/app.py", line 49, in   application
response = frappe.handler.handle()
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 66, in handle
execute_cmd(cmd)
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 89, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/__init__.py", line 531, in   call
return fn(*args, **newargs)
File "/home/adminuser/frappe-bench-hitech/apps/erpnext/erpnext/stock/doctype/item/item.py", line 405, in generate_barcode
last_barcode = last_barcode + 1
TypeError: can only concatenate tuple (not "int") to tuple
LaksHmiSekhar
  • 315
  • 2
  • 7
  • 16

3 Answers3

1

I don't know what "frappe" is and you failed to post the full traceback so we can only try and guess, but very obviously frappe.db.sql("select MAX(barcode) fromtabItem") returns a tuple (which is what I'd expect from a select on a SQL db), so you want something like:

row = frappe.db.sql(
    "select MAX(barcode) from `tabItem`"
    )

last_barcode = row[0]
if last_barcode:
    last_barcode = last_barcode + 1

As a side note: if you want a random int between 0 and 9 (included), it's spelled random.randint(0, 9)

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1

I somehow got the answer.Thanks for everyone's help.

@frappe.whitelist()
def generate_barcode():

last_barcode_auto = frappe.db.sql("""\
    select MAX(barcode) from `tabItem` """)

if last_barcode_auto[0][0] :
    last_barcode = last_barcode_auto[0][0]
    final_barcode= last_barcode+1

else:
    final_barcode=random.randrange(100001, 100000000000, 2)



return {'final_barcode':final_barcode}
LaksHmiSekhar
  • 315
  • 2
  • 7
  • 16
0

The error message is saying that last_barcode is a tuple. Use last_barcode[0] to retrieve the first value (which in this case would be the only value because you selected one column).

if last_barcode:
    last_barcode = last_barcode[0] + 1
Janne Karila
  • 24,266
  • 6
  • 53
  • 94