flask-sqlalchemy

MySQL Table not created using Flask and PyMySQL

泄露秘密 提交于 2019-12-08 09:23:24
问题 This maybe a duplicate, but all the solutions to the other questions did not help me. I am trying to modularize my code where I am moving my config files to a separate file called settings.py. To run the code i am going running from my terminal "python3 manage.py shell" Python not updating then I execute from flask_blog import db from author.models import Author db.create_all() Hence the QUESTION is: Why is the database not being updated? The three file I have that concern config are: manage

Flask-SqlAlchemy views reflection

余生颓废 提交于 2019-12-08 07:42:30
问题 SQLAlchemy class has the method reflect: reflect(bind='__all__', app=None) Reflects tables from the database. It has only 2 arguments: bind and app. I don't find any views in metadata.tables . Native SqlAlchemy's method reflect has more arguments and views=False is one of them allowing Views reflection if set up to True. reflect(bind=None, schema=None, views=False, only=None, extend_existing=False, autoload_replace=True, **dialect_kwargs) Is it possible to somehow reflect database views in

Reflecting different databases in Flask factory setup

一世执手 提交于 2019-12-08 04:52:31
问题 I'd like to use Flask's application factory mechanism fpr my application. I have is that the databases I use within some blueprints are located differently, so I'm using binds for pointing to them. The tables itself are in production and already in use, so I need to reflect them in order to use them within my application. Problem is that I can't get the reflect function working because of the application context. I always get the message, that I'm working outside the application context. I

A Flask-SQLAlchemy update is creating a new record in MySQL

自闭症网瘾萝莉.ら 提交于 2019-12-08 02:43:16
问题 I am trying to update a MySQL record from flask-sqlalchemy, but instead of updating the current record, it is adding a new one. But here's the really weird thing, when I run the same code from the Python prompt it updates properly without adding a new record. Here is the relevant portion of the code: First, the template that feeds the blog: {% for post in posts %} <article class="blog"> <header> <h2>{{ post.title|safe }}</h2> </header> <main> {{ post.content|safe }} </main> <footer> <p>{{

Updating multiple columns in Sqlalchemy

怎甘沉沦 提交于 2019-12-08 02:21:43
问题 I have an app that runs on flask and uses sqlalchemy to interact with the data base. I want to update the columns of a table with the user specified values. The query that I am using is def update_table(value1, value2, value3): query = update(Table).where(Table.column1 == value1).values(Table.column2 = value2, Table.column3 = value3) I am not sure if the way I am passing values is correct. Also the Table.column2/3 i=gives error saying Can't assign to function call . Where column2/3 are not

SQLAlchemy bulk update strategies

元气小坏坏 提交于 2019-12-08 01:48:40
问题 I am currently writing a web app (Flask) using SQLAlchemy (on GAE, connecting to Google's cloud MySQL) and needing to do bulk updates of a table. In short, a number of calculations are done resulting in a single value needing to be updated on 1000's of objects. At the moment I'm doing it all in a transaction, but still at the end, the flush/commit is taking ages. The table has an index on id and this is all carried out in a single transaction. So I believe I've avoided the usual mistakes, but

Flask-SQLAlchemy set relationship default value

青春壹個敷衍的年華 提交于 2019-12-07 21:35:56
问题 I have an app making by Flask and for database management I using Flask-admin and Flask-SQLAlchemy. And in my app there has three role, which is: admin , school parent here is the snipet of code on my table: roles_users = db.Table( 'roles_users', db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), db.Column('role_id', db.Integer(), db.ForeignKey('role.id')) ) class Role(db.Model, RoleMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True)

Auto incrementing a non-unique id upon creation using SQLAlchemy

狂风中的少年 提交于 2019-12-07 17:18:53
问题 My primary goal with this is to make implementing revision histories and journaling easier. I found myself wondering if it's possible, using Flask-SQLAlchemy (or just straight up SQL), to get an auto incremented non-unique integer for mysql. I found this stack overflow post which is close to what I want to do but the question is focused on a primary key. For example, if my table had these columns, revision_id = db.Column(db.Integer, nullable=False) post_id = db.Column(db.Integer, nullable

Ordering by subquery in SQLAlchemy

爱⌒轻易说出口 提交于 2019-12-07 16:23:17
问题 I'm trying to select the newest threads (Thread) ordered descending by the time of the most recent reply to them (the reply is a Post model, that's a standard forum query). In SQL I'd write it like this: SELECT * FROM thread AS t ORDER BY (SELECT MAX(posted_at) FROM post WHERE thread_id = t.id) DESC How do I do such thing in SQLAlchemy? I tried something like this: scalar = db.select[func.max(Post.posted_at)].where(Post.thread_id == Thread.id).as_scalar() threads = Thread.query.order_by

SQLAlchemy raises QueuePool limit of size 10 overflow 10 reached, connection timed out after some time

戏子无情 提交于 2019-12-07 14:05:00
问题 While using Flask-SQLAlchemy I get the error 'QueuePool limit of size 10 overflow 10 reached, connection timed out' consistently, after some time. I tried to increase connection pool size, but it only deferred the problem. def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) initialize_db(app) db = SQLAlchemy() def initialize_db(app): db.init_app(app) SQLALCHEMY_POOL_SIZE = 100 回答1: I figured out the problem. The