{"action":"created","issue":{"url":"https://api.github.com/repos/mitsuhiko/flask-sqlalchemy/issues/98","labels_url":"https://api.github.com/repos/mitsuhiko/flask-sqlalchemy/issues/98/labels{/name}","comments_url":"https://api.github.com/repos/mitsuhiko/flask-sqlalchemy/issues/98/comments","events_url":"https://api.github.com/repos/mitsuhiko/flask-sqlalchemy/issues/98/events","html_url":"https://github.com/mitsuhiko/flask-sqlalchemy/issues/98","id":6429516,"number":98,"title":"Document how to use plain SQLAlchemy models with the Flask-SQLAlchemy session","user":{"login":"dmckeone","id":614430,"avatar_url":"https://avatars.githubusercontent.com/u/614430?v=3","gravatar_id":"","url":"https://api.github.com/users/dmckeone","html_url":"https://github.com/dmckeone","followers_url":"https://api.github.com/users/dmckeone/followers","following_url":"https://api.github.com/users/dmckeone/following{/other_user}","gists_url":"https://api.github.com/users/dmckeone/gists{/gist_id}","starred_url":"https://api.github.com/users/dmckeone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmckeone/subscriptions","organizations_url":"https://api.github.com/users/dmckeone/orgs","repos_url":"https://api.github.com/users/dmckeone/repos","events_url":"https://api.github.com/users/dmckeone/events{/privacy}","received_events_url":"https://api.github.com/users/dmckeone/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":23,"created_at":"2012-08-24T09:40:10Z","updated_at":"2015-06-09T07:59:28Z","closed_at":"2015-06-09T07:59:28Z","body":"The application I was writing required that I have models that were portable (not just for use with Flask), but I still wanted to be able to take advantage of the sessions that Flask-SQLAlchemy provided.\r\n\r\nIt would be nice if it were documented somewhere that, if you are willing to forego some of the convenience methods on the Model (like Model.query), then you can use standard SQLAlchemy models with the Flask-SQLAlchemy session.\r\n\r\nIncidentally this approach also helps with avoiding circular imports when passing the SQLAlchemy() instance, db, around to each model.\r\n\r\nHere is an example:\r\n\r\n```python\r\n# models.py\r\n\r\nfrom sqlalchemy import Column, Integer, String\r\nfrom sqlalchemy.ext.declarative import declarative_base\r\n\r\nBase = declarative_base()\r\n\r\nclass User(Base):\r\n    __tablename__ = 'users'\r\n\r\n    id = Column(Integer, primary_key=True)\r\n    name = Column(String(50), unique=True)\r\n    email = Column(String(120), unique=True)\r\n\r\n    def __init__(self, name=None, email=None):\r\n        self.name = name\r\n        self.email = email\r\n\r\n    def __repr__(self):\r\n        return '<User %r>' % (self.name)\r\n```\r\n```python\r\n# app.py\r\n\r\nfrom flask import Flask\r\nfrom models import Base, User\r\nfrom flask_sqlalchemy import SQLAlchemy\r\n\r\napp =  Flask(__name__)\r\napp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'\r\ndb = SQLAlchemy(app)\r\n\r\n@app.before_first_request\r\ndef setup():\r\n    # Recreate database each time for demo\r\n    Base.metadata.drop_all(bind=db.engine)\r\n    Base.metadata.create_all(bind=db.engine)\r\n    db.session.add(User('Bob Jones', 'bob@gmail.com'))\r\n    db.session.add(User('Joe Quimby', 'eat@joes.com'))\r\n    db.session.commit()\r\n\r\n@app.route('/')\r\ndef root():\r\n    users = db.session.query(User).all()\r\n    return u\"<br>\".join([u\"{0}: {1}\".format(user.name, user.email) for user in users])\r\n\r\nif __name__ == '__main__':\r\n    app.run('127.0.0.1', 5000) \r\n```"},"comment":{"url":"https://api.github.com/repos/mitsuhiko/flask-sqlalchemy/issues/comments/110266204","html_url":"https://github.com/mitsuhiko/flask-sqlalchemy/issues/98#issuecomment-110266204","issue_url":"https://api.github.com/repos/mitsuhiko/flask-sqlalchemy/issues/98","id":110266204,"user":{"login":"immunda","id":524365,"avatar_url":"https://avatars.githubusercontent.com/u/524365?v=3","gravatar_id":"","url":"https://api.github.com/users/immunda","html_url":"https://github.com/immunda","followers_url":"https://api.github.com/users/immunda/followers","following_url":"https://api.github.com/users/immunda/following{/other_user}","gists_url":"https://api.github.com/users/immunda/gists{/gist_id}","starred_url":"https://api.github.com/users/immunda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/immunda/subscriptions","organizations_url":"https://api.github.com/users/immunda/orgs","repos_url":"https://api.github.com/users/immunda/repos","events_url":"https://api.github.com/users/immunda/events{/privacy}","received_events_url":"https://api.github.com/users/immunda/received_events","type":"User","site_admin":false},"created_at":"2015-06-09T07:59:28Z","updated_at":"2015-06-09T07:59:28Z","body":"Closing, inroads being made in #250"}}