title: accessing existing database in sqlalchemy in flask project + sqlalchemy filter
body: <p><strong>First:</strong></p>
  
  <p>I'm working on a flask project and using msql for now, but want to migrate to sqlalchemy. I want to access existing database table using sqlalchemy. Does sqlalchemy have any alternative for something like this</p>
  
  <blockquote>
    <p>python manage.py inspectdb > models.py</p>
  </blockquote>
  
  <p>which we use in django.</p>
  
  <p>I tried to test <a href="https://stackoverflow.com/a/30404103/6662140">this solution</a>, so my script is like this now:</p>
  
  <pre><code>from flask import Flask, render_template
  from itertools import groupby
  from flask import request
  import MySQLdb
  from flask_sqlalchemy import SQLAlchemy
  
  from sqlalchemy.ext.automap import automap_base
  from sqlalchemy.orm import Session
  from sqlalchemy import create_engine
  
  
  Base = automap_base()
  engine = create_engine("mysql://myusername:mypassword@localhost/mydbname")
  Base.prepare(engine, reflect=True)
  User = Base.classes.p_user
  session = Session(engine)
  session.commit()
  
  
  app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://myusername:mypassword@localhost/mydbname'
  db = SQLAlchemy(app)
  
  
  
  
  
  @app.route('/test/')
  def test():
      result = str(session.query(User).all())
      return result
  
  
  if __name__ == '__main__':
      app.run()
  </code></pre>
  
  <p>But the output is like this:</p>
  
  <blockquote>
    <p>[, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ]</p>
  </blockquote>
  
  <p>Why? And how I can fix it?</p>
  
  <p><strong>Second:</strong></p>
  
  <p>Also how should I use filter for selecting data in my script?</p>
  
  <p>I tried this:</p>
  
  <pre><code>result = session.query(User).filter_by(shahr = 'tehran')
  </code></pre>
  
  <p>But this arror raised in terminal:</p>
  
  <blockquote>
    <p>TypeError: 'Query' object is not callable</p>
  </blockquote>
  
  <p>Also tried this one:</p>
  
  <pre><code>result = session.query.filter_by(shahr = 'tehran')
  </code></pre>
  
  <p>The error:</p>
  
  <blockquote>
    <p>AttributeError: 'function' object has no attribute 'filter_by'</p>
  </blockquote>
  
  <p>Or tried this one:</p>
  
  <pre><code>result = User.query.filter_by(shahr = 'tehran')
  </code></pre>
  
  <p>And the error was:</p>
  
  <blockquote>
    <p>AttributeError: type object 'p_user' has no attribute 'query'</p>
  </blockquote>