Search code examples
pythonmysqlpeeweeflask-peewee

Unwanted chars are added after saving string to MySQL database


I need to update an existing entry in my MySQL database with the help of peewee and experienced a quite weird issue.

When I save a number as a string, some new chars will be added after I saved it.

For example when I run the below code:

obj = mydbtable.get(mydbtable.zid == zid)
myval = "10"
obj.myfield = myval
obj.save()

I get the following string in my PhpMyAdmin:

(10,)

So the value of myval will be (10,) after saving instead of 10.

Honestly I have no idea what causes this issue, because I have done the same saving task several times and worked like a charm always. I tried to pass the variable even as an int but got the same.

Update

Right after saving obj, calling the below method fixes the problem, but this workaround is a bit overkill and not the most elegant solution:

    objy = mydbtable.get(mydbtable.zid == zid)
    ooo = objy.myfield
    objy.myfield = ooo.replace(',','').replace(')','').replace('(','').replace("'","")
    objy.save()

Solution

  • Looks like you're assigning a tuple to the field instead of a string or number. I'm not sure where your source data is coming from but check for trailing commas if nothing else.