我尝试使用Flask和SQLAlchemy建立一个API。
此处的示例:https://github.com/miguelgrinberg/REST-auth/blob/master/api.py
现在这些是我的两个班级:
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), index=True) #, unique=True)
firstname = db.Column(db.String(50))
lastname = db.Column(db.String(50))
password_hash = db.Column(db.String(64))
bt_ids = db.relationship("BT", order_by="BT.id", backref="user")
def hash_password(self, password):
self.password_hash = pwd_context.encrypt(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password_hash)
def generate_auth_token(self, expiration=600):
s = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
return s.dumps({'id': self.id})
@staticmethod
def verify_auth_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except SignatureExpired:
return None # valid token, but expired
except BadSignature:
return None # invalid token
user = User.query.get(data['id'])
return user
class BT(db.Model):
__tablename__ = 'bt'
id = db.Column(db.Integer, primary_key=True)
bt_id = db.Column(db.String(255), unique=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
user = db.relationship("User", backref=db.backref('bt_ids', order_by=id))
但是当我做这样的事情来查询它时:
qemail = User.query.filter_by(email =' test@test.com')。first()
我收到 500内部服务器错误
当我打开Flask_Debug时,这是信息:
sqlalchemy.exc.ArgumentError
ArgumentError: Error creating backref 'user' on relationship 'User.bt_ids': property of that name exists on mapper 'Mapper|BT|bt'
Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\kwmaster\Documents\Forx\RestAPI2", line 97, in new_user
email7 = User.query.filter_by(email='kai.wieland@gmail.com').first()
File "C:\Python27\lib\site-packages\flask_sqlalchemy\__init__.py", line 452, in __get__
mapper = orm.class_mapper(type)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\base.py", line 412, in class_mapper
mapper = _inspect_mapped_class(class_, configure=configure)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\base.py", line 391, in _inspect_mapped_class
mapper._configure_all()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 1156, in _configure_all
configure_mappers()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 2589, in configure_mappers
mapper._post_configure_properties()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 1694, in _post_configure_properties
prop.init()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\interfaces.py", line 144, in init
self.do_init()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\relationships.py", line 1553, in do_init
self._generate_backref()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\relationships.py", line 1743, in _generate_backref
(backref_key, self, m))
ArgumentError: Error creating backref 'user' on relationship 'User.bt_ids': property of that name exists on mapper 'Mapper|BT|bt'
答案 0 :(得分:0)
您的user
模型中已经有一个名为BT
的属性,并且您正在尝试通过backref创建另一个属性。你应该删除
bt_ids = db.relationship("BT", order_by="BT.id", backref="user")
或将backref名称更改为唯一。