After many many attempts at integrating sqlalchemy orm into my rest server for my app, I've found myself again banging my head against a wall after having stripped everything down to be as bare as possible, and having it still not work. I've followed different guides online, and I can't find anything that I did differently, but using the code below results with "TypeError: 'Query' object is not callable", I've also gotten "TypeError: 'Item' object is not callable" Can anyone shed some light on what I'm missing here? I have a working version up and running using raw SQL calls on different database "engine" objects, but I'd really like to be able to grasp and utilize the orm side of SQLAlchemy
#app.py
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Item
from database import db_session, init_db
app = Flask(__name__)
db = SQLAlchemy(app)
init_db()
@app.teardown_appcontext
def shutdown_session(exception=None):
db_session.remove()
@app.route('/')
def hello_world():
#This didn't work.
#item = Item.query.filter(Item.name == 'test').first()
item = db_session.query(Item).filter(Item.name == 'test')
return item
#models.py
from sqlalchemy import Column, DateTime, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from database import Base
class Item(Base):
__tablename__= 'items'
id = Column(Integer, primary_key=True)
number = Column(Integer)
name = Column(String(30))
def __init__(self, number=None, name=None):
self.number = number
self.name = name
def __repr__(self):
return '<Item %r>' % (self.name)
#database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://NAME:PASS@StillMyName.mysql.pythonanywhere-services.com/MyName:TheDatabase', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
from models import Item
Base.metadata.create_all(bind=engine)
print("****************************************************************************Database Initiated********************************************")