0

I'm trying to insert some values into my database using Flask Restful. I'm using PyMysql with Flask SQlAlchemy for database connection but when I insert, I'm getting the

AttributeError: 'function' object has no attribute 'translate'

when I do

db.session.add(self)
db.session.commit()

Error

File "/home/yung/Documents/Projects/Crowlabs/Projects/churchify/venv/lib/python2.7/site-packages/pymysql/converters.py", line 73, in _escape_unicode

def _escape_unicode(value, mapping=None):
    """escapes *value* without adding quote.

    Value should be unicode
    """
    return value.translate(_escape_table)

    if PY2:
        def escape_string(value, mapping=None):
            """escape_string escapes *value* but not surround it with quotes.

My Code

from db import db

class Test(db.Model):
    __tablename__       = "auth_test"

    id                  = db.Column(db.Integer, primary_key=True)
    username            = db.Column(db.Text)
    password            = db.Column(db.Text)
    email               = db.Column(db.Text)
    type                = db.Column(db.Text)

    def __init__(self, username, password, email, type_):
        self.username       = username
        self.password       = password
        self.email          = email
        self.type           = type_

        db.session.add(self)
        db.session.commit()

This only comes up when I try to insert into the database but when retrieving it works well

3
  • Not very familiar with Python but found another answer that might point you in the right direction - stackoverflow.com/a/36931430/499528 Commented May 20, 2018 at 0:03
  • 2
    what is value in your code? show us the code where you are using the escape_unicode function Commented May 20, 2018 at 0:22
  • The escape_unicode function is from pymysql Commented May 20, 2018 at 5:44

1 Answer 1

1

So I finally got it. Pymysql was expecting a string value for one of my values while I was using in integer so what I did was to convert the values to string using

str()

And it worked

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.