0
我想創建一個簡單的應用程序使用燒瓶和sqlalchemy鏈接到MySQL數據庫。我在我的數據庫中創建了一個包含一些虛擬數據的用戶表,並試圖在我的/ api路由上測試一個示例查詢。我不斷收到內部服務器錯誤,而且我的生活無法弄清楚發生了什麼。我以前製作過類似的應用程序,它從來沒有給我這個奇怪的錯誤。堆棧跟蹤如下:與sqlalchemy的神祕錯誤:TypeError'用戶'對象不可調用
2016-12-02 22:15:16,079 - app - ERROR - Exception on /api [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1642, in full_dispatch_request
response = self.make_response(rv)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1746, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 847, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/test.py", line 871, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'User' object is not callable
app.py
from flask import Flask
from database.models import User
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import create_engine
engine = create_engine('mysql://root:[email protected]/testdb', convert_unicode=True)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create a scoped session
ssession = scoped_session(Session)
app = Flask(__name__)
# Setup our error logging
if app.debug is not True:
import logging
from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler('errors.log', maxBytes=1024 * 1024 * 100, backupCount=20)
file_handler.setLevel(logging.ERROR)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
app.logger.addHandler(file_handler)
@app.route('/')
def index():
return 'hello world'
@app.route('/api')
def api_route():
# Error is happening right here
return ssession.query(User).first()
if __name__ == '__main__':
app.run(debug=False)
數據庫/ models.py
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, BLOB, \
DateTime, ForeignKey
Base = declarative_base()
class User(Base):
__tablename__ = 'User'
id = Column(Integer, primary_key=True)
email = Column(String(64), unique=True)
password = Column(String(255))
join_date = Column(DateTime)
last_login = Column(DateTime)
login_count = Column(Integer, default=0)
name = Column(String(128), default=None)
location = Column(String(128), default=None)
points = Column(Integer, default=0)
num_posts = Column(Integer, default=0)
def __init__(self, email, password):
self.email = email
self.password = password
self.join_date = datetime.utcnow()
self.last_login = datetime.utcnow()
def __repr__(self):
return '<User %r>' % self.email
我完全不知道自己爲什麼這是不工作 - 如果我導入所有必要的模塊,它實際上在python控制檯中工作得很好!任何見解都會很棒。