我有两个文件,restmodels.py和routes.py
restmodels.py代码如下
__author__ = 'seroney'
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
class ProductAttributes(db.Model):
__tablename__ = 'oc_product_attribute'
product_id = db.Column(db.Integer,primary_key=True)
attribute_id = db.Column(db.Integer,primary_key=True)
language_id = db.Column(db.Integer,primary_key=True)
text=db.Column(db.String)
和routes.py的代码如下。
from flask import Flask,request,jsonify
from flask_sqlalchemy import SQLAlchemy
import requests
from restmodels import ProductAttributes
app = Flask(__name__)
#manager = flask_restless.APIManager(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:seroney@localhost/test'
db = SQLAlchemy(app)
@app.route('/api/attributes/',methods=['GET'])
def getProductAttributes():
if request.method =='GET':
results = ProductAttributes.query.all()
json_results = []
for result in results:
d = {
'product_id':result.product_id,
'attribute_id':result.attribute_id,
'language_id':result.language_id,
'text':result.text
}
json_results.append(d)
return jsonify(items = json_results)
@app.route('/api/attributes/', methods=['POST'])
def postProductAttributes():
# if request.headers['Content-Type'] == 'application/json':
product_id = request.get_json('product_id')
attribute_id = request.get_json('attribute_id')
language_id = request.get_json('language_id')
text = request.get_json('text')
# if product_id is None or attribute_id is None or language_id is None or text is None:
# return jsonify({"message": "Error."}), 400
#new_attrib = Attributes(text=text,product_id=product_id,language_id=language_id,attribute_id=attribute_id)
print request.json
dict = request.json
product_id = dict['product_id']
attribute_id = dict['attribute_id']
if ProductAttributes.query.filter_by(attribute_id=product_id,product_id=product_id) is None:
print ProductAttributes.query.filter_by(attribute_id=attribute_id,product_id = product_id)
print "Product_id = " +str(product_id)
print "Attribute_id = "+str(attribute_id)
return jsonify({"message": "Error product attribute already exists."}), 400
db.session.add(ProductAttributes(**request.json))
db.session.commit()
print requests.Response()
#return 'ok'
return jsonify(**request.json),201
if __name__ == '__main__':
app.run(debug=True)
我正在使用mysql数据库,该表存在于下面的快照中
当我收到请求时,我收到以下错误
OperationalError: (OperationalError) no such table: oc_product_attribute u'SELECT oc_product_attribute.product_id AS oc_product_attribute_product_id, oc_product_attribute.attribute_id AS oc_product_attribute_attribute_id, oc_product_attribute.language_id AS oc_product_attribute_language_id, oc_product_attribute.text AS oc_product_attribute_text \nFROM oc_product_attribute' () // Werkzeug Debugger
但是当我在与routes.py相同的文件中包含restmodels.py代码时,代码可以正常工作,但对于POST请求的上述代码,代码可以正常工作。非常感谢任何帮助