我需要爲我的Flask應用程序實現一個API,並已看到Flask-Restless
的建議。我已經跑進了一堵牆,只是基本使用了這個圖書館,並希望有人使用它可以提供幫助。Flask-Restless GET請求拋出SQLAlchemy錯誤
創建API管理和端點...
manager = APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Gallery, methods=['GET', 'POST', 'PUT', 'DELETE'])
...這與
class Gallery(db.Model):
__tablename__ = 'galleries'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(25))
def __init__(self, title):
self.title = title
去現在,插入新條目的模式,我用這個jQuery的AJAX POST請求。這工作正常。我已經在sqlite數據庫查看器中測試過它,並可以看到條目。
$('#form').on('submit', function(e){
$.ajax({
type: 'post',
url: '/api/galleries',
contentType:"binary/octet-stream",
data: JSON.stringify({'title': $('#title').val()}),
success: function(){
alert('Success!');
}
});
e.preventDefault();
});
這是問題所在。我去http://localhost:5000/api/galleries
在我的瀏覽器,並獲得以下消息:
sqlalchemy.orm.exc.NoResultFound
NoResultFound:沒有行被發現的一個()
它的奇怪它會當api/galleries
應該返回一個數據庫條目列表時,嘗試運行一個()。我嘗試api/galleries/1
並得到完全相同的錯誤。我加倍檢查,主ID是1.我錯過了什麼?
調試它並逐步跟蹤以查找運行的語句。 – codecool