Flask SQLAlchemy:Atrribute错误

时间:2018-05-19 23:57:06

标签: python flask flask-sqlalchemy flask-restful pymysql

我正在尝试使用Flask Restful将一些值插入到我的数据库中。我正在使用Flask SQlAlchemy的PyMysql进行数据库连接,但是当我插入时,我正在使用

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

当我做的时候

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

错误

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.

我的代码

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()

只有当我尝试插入数据库但是检索它时效果很好

1 个答案:

答案 0 :(得分:0)

所以我终于明白了。当我使用整数时,Pymysql期望我的一个值的字符串值,所以我做的是使用

将值转换为字符串
str()

它有效