moluwole
moluwole

Reputation: 21

Flask SQLAlchemy: Attribute Error: 'function' object has no attribute 'translate'

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

Upvotes: 0

Views: 1613

Answers (1)

moluwole
moluwole

Reputation: 21

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

Upvotes: 1

Related Questions