flask-sqlalchemy

how to store binary file recieved by Flask into postgres

风格不统一 提交于 2019-12-30 07:17:08
问题 I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route('/upload', methods=['POST']) def upload_file(): def allowed_file(f): return True file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(upload_dir(), filename)) return "", 200 I would like to store it in a BYTEA column in postgres, I am not sure how to bind the "data" argument to the insert

Flask SQLAlchemy pagination error

天大地大妈咪最大 提交于 2019-12-30 06:36:36
问题 I have this code and the all() method and every other method works on this and I have looked all over and I could that the method paginate() works on BaseQuery which is also Query @app.route('/') @app.route('/index') @app.route('/blog') @app.route('/index/<int:page>') def index(page = 1): posts = db.session.query(models.Post).paginate(page, RESULTS_PER_PAGE, False) return render_template('index.html', title="Home", posts=posts) but this gives me the error AttributeError: 'Query' object has no

Flask SQLAlchemy pagination error

人盡茶涼 提交于 2019-12-30 06:36:25
问题 I have this code and the all() method and every other method works on this and I have looked all over and I could that the method paginate() works on BaseQuery which is also Query @app.route('/') @app.route('/index') @app.route('/blog') @app.route('/index/<int:page>') def index(page = 1): posts = db.session.query(models.Post).paginate(page, RESULTS_PER_PAGE, False) return render_template('index.html', title="Home", posts=posts) but this gives me the error AttributeError: 'Query' object has no

SQLALCHEMY_DATABASE_URI not set

倖福魔咒の 提交于 2019-12-30 05:56:20
问题 I tried to work with CURD operation using Flask and SQLAlchemy .But getting Error while connecting to database. Here is the Error log. /usr/local/lib/python3.4/dist-packages/flask_sqlalchemy/__init__.py:819: UserWarning: SQLALCHEMY_DATABASE_URI not set. Defaulting to "sqlite:///:memory:". 'SQLALCHEMY_DATABASE_URI not set. Defaulting to ' /usr/local/lib/python3.4/dist-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and

Flask Marshmallow/SqlAlchemy: Serializing many-to-many relationships

点点圈 提交于 2019-12-30 04:45:07
问题 I'm building a small REST api using Flask, flask-sqlalchemy and flask-marshmallow. For some requests I'd like to return a json serialized response consisting of my sqlalchemy objects. However I cant get the serialization to work with eagerly loaded sqlalchemy objects when using many-to-many relationships / secondary tables. Here is a simple example, more or less copy/pasted from flask-marshmallow docs: from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_marshmallow

No changes detected in Alembic autogeneration of migrations with Flask-SQLAlchemy

跟風遠走 提交于 2019-12-29 20:17:12
问题 I'm having trouble getting Alembic to autogenerate candidate migrations from changes to classes using db.Model (Flask-SQLAlchemy) instead of Base . I've modified env.py to create my Flask app, import all relevant models, initialize the database, and then run migrations: ... uri = 'mysql://user:password@host/dbname?charset=utf8' app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = uri app.config['SQLALCHEMY_ECHO'] = True db.init_app(app) with app.test_request_context(): target

Cross database join in sqlalchemy

人盡茶涼 提交于 2019-12-29 04:37:23
问题 Is there a way in SQLAlchemy to do cross-database joins. To be specific, here is my use case: Schema db1.entity1 entity1_id: Primary Key entity2_id: Foreign Key to db2.entity2.entity2_id db2.entity2 entity2_id: Primary Key Model I'm using declarative style for models. class Entity1(Base): __tablename__ = 'entity1' ## I tried combination of <db>.<table> with no success entity1_id = Column(Integer, primary_key=True) entity2_id = Column(Integer, ForeignKey('db2.entity2.entity2_id')) entity2 =

How to delete a record by id in Flask-SQLAlchemy

房东的猫 提交于 2019-12-28 08:04:35
问题 I have users table in my MySql database. This table has id , name and age fields. How can I delete some record by id ? Now I use the following code: user = User.query.get(id) db.session.delete(user) db.session.commit() But I don't want to make any query before delete operation. Is there any way to do this? I know, I can use db.engine.execute("delete from users where id=...") , but I would like to use delete() method. 回答1: You can do this, User.query.filter_by(id=123).delete() or User.query

Flask-SQLAlchemy import/context issue

元气小坏坏 提交于 2019-12-27 11:34:38
问题 I want to structure my Flask app something like: ./site.py ./apps/members/__init__.py ./apps/members/models.py apps.members is a Flask Blueprint. Now, in order to create the model classes I need to have a hold of the app, something like: # apps.members.models from flask import current_app from flaskext.sqlalchemy import SQLAlchemy db = SQLAlchemy(current_app) class Member(db.Model): # fields here pass But if I try and import that model into my Blueprint app, I get the dreaded RuntimeError:

Flask-SQLAlchemy import/context issue

允我心安 提交于 2019-12-27 11:34:07
问题 I want to structure my Flask app something like: ./site.py ./apps/members/__init__.py ./apps/members/models.py apps.members is a Flask Blueprint. Now, in order to create the model classes I need to have a hold of the app, something like: # apps.members.models from flask import current_app from flaskext.sqlalchemy import SQLAlchemy db = SQLAlchemy(current_app) class Member(db.Model): # fields here pass But if I try and import that model into my Blueprint app, I get the dreaded RuntimeError: