flask-sqlalchemy

read slave , read-write master setup

£可爱£侵袭症+ 提交于 2019-11-27 09:53:18
问题 I have a Flask,SQLAlchemy webapp which uses a single mysql server. I want to expand the database setup to have a read-only slave server such that I can spread the reads between both master and slave while continuing to write to the master db server. I have looked at few of options and I believe I can't do this with plain SQLAlchemy. Instead Im planning to create 2 database handles in my webapp, one each for master and slave db servers. Then using a simple random value use either the master

Can't set attribute on result objects in SQLAlchemy flask

≯℡__Kan透↙ 提交于 2019-11-27 09:50:13
I encounter a problem with Flask-SQLAlchemy, I can set the attribute of the objects in place_collections , but when I want to set the attribute for objects in places , an error occurred: Traceback (most recent call last): File "/Users/user/PycharmProjects/website/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "/Users/user/PycharmProjects/website/venv/lib/python3.6/site-packages/werkzeug/contrib/fixers.py", line 152, in __call__ return self.app(environ, start_response) File "/Users/user/PycharmProjects/website/venv/lib

How do I produce nested JSON from database query with joins? Using Python / SQLAlchemy

纵然是瞬间 提交于 2019-11-27 08:46:30
问题 I have a specify use case but my question pertains to the best way of doing this in general. I have three tables Order - primary key order_id OrderLine - Linking table with order_id, product_id and quantity. An order has 1 or more order lines Product - primary key product_id, each order line has one product In sqlachemy / python how do I generate nested JSON along the lines of: { "orders": [ { "order_id": 1 "some_order_level_detail": "Kansas" "order_lines": [ { "product_id": 1, "product_name"

Get inserted key before commit session

雨燕双飞 提交于 2019-11-27 08:03:21
class Parent(db.Model): id = db.Column(db.Integer, primary_key=True) class Child(db.Model): id = db.Column(db.Integer, primary_key=True) parent_id = db.Column(db.Integer, db.ForeignKey('parent.id')) parent = Parent() db.session.add(parent) child = Child() child.parent_id = parent.id db.session.add(child) db.session.commit() I want to INSERT into both parent and child tables inside a session considering that the parent_id must be included in the child table. In the moment I create the child object, parent.id is None . How can I achieve that? You could use flush() to flush changes to the

Using DATEADD in sqlalchemy

本秂侑毒 提交于 2019-11-27 06:54:34
问题 How can I rewrite the following sql statement with sqlalchemy in python. I have been searching for 30 mins but still couldn't find any solutions. DATEADD(NOW(), INTERVAL 1 DAY) or INSERT INTO dates (expire) VALUES(DATEADD(NOW(), INTERVAL 1 DAY)) Thanks in advance 回答1: SQLAlchemy dates automagically map to Python datetime objects, so you should just be able to do: from sqlalchemy import Table, Column, MetaData, DateTime from datetime import datetime, timedelta metadata = MetaData() example =

SQLAlchemy ManyToMany secondary table with additional fields

做~自己de王妃 提交于 2019-11-27 06:16:24
I have 3 tables: User, Community, community_members (for relationship many2many of users and community). I create this tables using Flask-SQLAlchemy: community_members = db.Table('community_members', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('community_id', db.Integer, db.ForeignKey('community.id')), ) class Community(db.Model): __tablename__ = 'community' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False, unique=True) members = db.relationship(User, secondary=community_members, backref=db.backref('community_members', lazy=

How to update SQLAlchemy row entry?

和自甴很熟 提交于 2019-11-27 05:58:56
Assume table has three columns: username , password and no_of_logins . When user tries to login, it's checked for an entry with a query like user=User.query.filter_by(username=form.username.data).first() If password matches, he proceeds further. What I would like to do is count how many times the user logged in. Thus whenever he successfully logs in, I would like to increment the no_of_logins field and store it back to the user table. I'm not sure how to run update query with SqlAlchemy. user.no_of_logins += 1 session.commit() Nima Soroush There are several ways to UPDATE using sqlalchemy 1)

SQLAlchemy ORM conversion to pandas DataFrame

旧巷老猫 提交于 2019-11-27 05:57:46
This topic hasn't been addressed in a while, here or elsewhere. Is there a solution converting a SQLAlchemy <Query object> to a pandas DataFrame? Pandas has the capability to use pandas.read_sql but this requires use of raw SQL. I have two reasons for wanting to avoid it: 1) I already have everything using the ORM (a good reason in and of itself) and 2) I'm using python lists as part of the query (eg: .db.session.query(Item).filter(Item.symbol.in_(add_symbols) where Item is my model class and add_symbols is a list). This is the equivalent of SQL SELECT ... from ... WHERE ... IN . Is anything

Flask Download a File

老子叫甜甜 提交于 2019-11-27 05:27:48
问题 I'm trying to create a web app with Flask that lets a user upload a file and serve them to another user. Right now, I can upload the file to the upload_folder correctly. But I can't seem to find a way to let the user download it back. I'm storing the name of the filename into a database. I have a view serving the database objects. I can delete them too. @app.route('/dashboard', methods=['GET', 'POST']) def dashboard(): problemes = Probleme.query.all() if 'user' not in session: return redirect

Implementing Flask-Login with multiple User Classes

杀马特。学长 韩版系。学妹 提交于 2019-11-27 05:23:18
问题 I am writing an app that has multiple classes that function as Users (for example, a School Account and a Staff account). I'm trying to use Flask-Login to make this easy but I'm not quite sure how to make it, so that when a user logs in I can have my app check to see whether or not the username belongs to a School account or Staff account, and then log in appropriately. I know how to figure out which type of account it belongs to (since all usernames must be unique). But after that I'm not