1

在我的 Flask-Restplus 中实现 Flask-SQLAlchemy 时,我遇到了一个问题。它始终显示错误:

sqlalchemy.orm.exc.UnmappedInstanceError: Class '__main__.User' is not mapped

下面我写的代码是github上Flask-Restplus示例项目的简化版(1个文件):https ://github.com/postrational/rest_api_demo

我一遍又一遍地检查,但不知道为什么仍然出现错误。

这是我的代码:

from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
from flask_restplus import Resource, Api, fields

app = Flask(__name__)
api = Api(app, version='v0.1')
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)

ns = api.namespace('users', description='User API')

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(80))
    password = db.Column(db.String(80))

    def __init__(self, email, password):
        self.email = email
        self.password = password

    def __repr__(self):
        return '<User %s>' % self.email


user = api.model('user', {
    "id": fields.Integer(readOnly=True),
    "email": fields.String(required=True),
    "password": fields.String(required=True),
})


def create_user(data):
    email = data.get('email')
    password = data.get('password')

    user = User(email, password)

    db.session.add(user)
    db.session.commit()

def update_user(user_id, data):
    user = User.query.filter(User.id == user_id).first()
    user.email = data.get('email')
    user.password = data.get('password')

    db.session.add(user)
    db.session.commit()

def delete_user(user_id):
    user = User.query.filter(User.id == user_id).first()

    db.session.delete(user)
    db.session.commit()


@ns.route('/')
class UserList(Resource):
    @ns.marshal_list_with(user)
    def get(self):
        users = User.query.all()
        return users

    @ns.marshal_with(user)
    @ns.expect(user)
    def post(self):
        data = request.json
        create_user(data)
        return None, 201

@ns.route('/<int:id>')
class User(Resource):
    @ns.marshal_with(user)
    def get(self, id):
        user = User.query.filter(User.id == id).first()
        return user

    @ns.expect(user)
    @ns.marshal_with(user)
    def put(self, id):
        data = request.json
        user = update_user(id, data)
        return None, 204

    def delete(self, id):
        delete_user(id)
        return None, 204


db.create_all()

user = User(email= 'saffsds@gmail.com', password= '4324dsfdsfa')
db.session.add(user)
db.session.commit()

user = User(email= 'sdfsdf@gmail.com', password= '5435rs')
db.session.add(user)
db.session.commit()

users = User.query.all()
print users

if __name__ == "__main__":

    app.run(debug=True)

我在代码上有什么遗漏吗?反正我用python2.7..

4

1 回答 1

0

我很确定它是因为你正在重载User.__init__,但你没有调用 super,所以这样做并查看它是否映射:

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(80))
    password = db.Column(db.String(80))

    def __init__(self, email, password):
        self.email = email
        self.password = password
        super(User,self).__init__()

    def __repr__(self):
        return '<User %s>' % self.email
于 2017-03-17T23:07:49.520 回答