ImportError: No module named sqlalchemy
Did you install flask-sqlalchemy? It looks like you have SQLAlchemy installed but not the Flask extension. Try pip install Flask-SQLAlchemy in your project’s virtualenv to install it from PyPI.
Did you install flask-sqlalchemy? It looks like you have SQLAlchemy installed but not the Flask extension. Try pip install Flask-SQLAlchemy in your project’s virtualenv to install it from PyPI.
Use query.one() to get one, and exactly one result. In all other cases it will raise an exception you can handle: from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import MultipleResultsFound try: user = session.query(User).one() except MultipleResultsFound, e: print e # Deal with it except NoResultFound, e: print e # Deal with that as well There’s also … Read more
There is an upsert-esque operation in SQLAlchemy: db.session.merge() After I found this command, I was able to perform upserts, but it is worth mentioning that this operation is slow for a bulk “upsert”. The alternative is to get a list of the primary keys you would like to upsert, and query the database for any … Read more
The URI should start with postgresql:// instead of postgres://. SQLAlchemy used to accept both, but has removed support for the postgres name.
That snippet is really old. flaskext is no more (or at least very deprecated). Refer to packages directly rather than through flaskext or flask.ext. from flask_sqlalchemy import SQLAlchemy Flask-SQLAlchemy (and most other extensions) no longer register in the flaskext namespace, and flask.ext was deprecated then removed in 1.0. The only correct way to refer to … Read more
the problem is this: post = Post(body=form.body.data, timestamp=datetime.utcnow(), thread=thread.id, author=g.user.id) you want to work with ORM objects, not primary key columns: post = Post(body=form.body.data, timestamp=datetime.utcnow(), thread=thread, author=g.user) the error means that an integer is being interpreted as an ORM object.
Model.query is a shortcut to db.session.query(Model), it’s not callable. If you’re not querying a model, continue to use db.session.query(…) as you would with regular SQLAlchemy. db.session.query(db.func.sum(Services.price)).filter( Services.dateAdd.between(start, end) )
You could use flush() to flush changes to the database and thus have your primary-key field updated: parent = Parent() db.session.add(parent) db.session.flush() print parent.id # after flush(), parent object would be automatically # assigned with a unique primary key to its id field child = Child() child.parent_id = parent.id db.session.add(child)
As of Flask-SQLAlchemy 3.0, all access to db.engine (and db.session) requires an active Flask application context. db.create_all uses db.engine, so it requires an app context. with app.app_context(): db.create_all() When Flask handles requests or runs CLI commands, a context is automatically pushed. You only need to push one manually outside of those situations, such as while … Read more
The underlying db-api library for whatever database you’re using (sqlite3, psycopg2, etc.) escapes parameters. SQLAlchemy simply passes the statement and parameters to execute, the driver does whatever is needed. Assuming you are not writing raw SQL that includes parameters yourself, you are not vulnerable to injection. Your example is not vulnerable to injection.