flask-sqlalchemy

Flask SQL-Alchemy | MySql - Multiple Foreign Keys issues

我的梦境 提交于 2019-12-06 07:40:13
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) class Devices(db.Model): __tablename__ = 'devices' id = db.Column(db.Integer, primary_key=True) purpose = db.Column('purpose', db.String(64)) type = db.Column('type', db.String(64)) name = db.Column('name', db.String(64)) channel = db.Column('channel', db.Integer) role_id = db.Column('role_id', db.Integer) role_permissions = db.Column('role_permissions', db.Integer)

Populate second dropdown based on the value selected in the first dropdown in flask using ajax and jQuery

不打扰是莪最后的温柔 提交于 2019-12-06 07:20:01
问题 I am not getting anywhere on this. Any help regarding this would be greatly appreciated! I have two dropdowns for Managers and Employees. Managers dropdown contains a list of Managers by default. I want to populate Employees dropdown with the employees name by querying a SQL Server database with the Manager name a user selects in Managers dropdown. So for e.g. if a person selects Tom as the Manager in the Managers dropdown then the Employees dropdown should populate with the Employees name

Sort by Count of Many to Many Relationship - SQLAlchemy

北慕城南 提交于 2019-12-06 06:40:25
I am using Flask-SQLAlchemy to to query my Postgres database. I am currently trying to query for suggestions of titles with the following query: res = Title.query.filter(Titles.name.ilike(searchstring)).limit(20) So far so good. Now I would like to order the results by the number of "subscribers" each Title object has. I am aware of the following SO question: SQLAlchemy ordering by count on a many to many relationship however its solution did not work for me. I am receiving the following error: ProgrammingError: (ProgrammingError) column "publishers_1.id" must appear in the GROUP BY clause or

Reset id count after table delete()

不打扰是莪最后的温柔 提交于 2019-12-06 05:39:54
问题 For testing purposes, I clear (delete) every table before executing code. for table in reversed(db.metadata.sorted_tables): engine.execute(table.delete()) do_stuff() However, the new data's id values start from where the previous id 's left off: First iteration: id | value -----+--------- 1 | hi 2 | there Second iteration (delete table, insert new data): id | value -----+--------- 3 | good 4 | day Is there any way to reset the id count when I delete the table? EDIT: seems I broke it, table is

Using different binds in the same class in Flask-SQLAlchemy

末鹿安然 提交于 2019-12-06 05:19:10
问题 I currently have multiple databases with identical Tables and Columns (but different data inside). So clearly I need to use binds to access all of them, but it's apparently not as simple as doing this: class WhateverTable(db.Model): __tablename__ = 'whatevertable' whatever = db.Column(db.String(255)) def __init__(self, bind=None): self.__bind_key__ = bind and then later calling: WhateverTable(bind='bind_key_here').query.filter_by(whatever='whatever').first() Is there a way I can do this

How to access the orm with celery tasks?

三世轮回 提交于 2019-12-06 05:16:31
问题 I'm trying to flip a boolean flag for particular types of objects in my database using sqlalchemy+celery beats. But how do I access my orm from the tasks.py file? from models import Book from celery.decorators import periodic_task from application import create_celery_app celery = create_celery_app() # Create celery: http://flask.pocoo.org/docs/0.10/patterns/celery/ # This task works fine @celery.task def celery_send_email(to,subject,template): with current_app.app_context(): msg = Message(

How do I get the number of rows affected with SQL Alchemy?

怎甘沉沦 提交于 2019-12-06 04:22:36
问题 How do I get the number of rows affected for an update statement with sqlalchemy? (I am using mysql and python/pyramid): from sqlalchemy.engine.base import ResultProxy @classmethod def myupdate(cls, id, myvalue): DBSession.query(cls).filter(cls.id == id).update({'mycolumn': myvalue}) if ResultProxy.rowcount == 1: return True else: return False Note: I saw this post but according to the docs: "The ‘rowcount’ reports the number of rows matched by the WHERE criterion of an UPDATE or DELETE

Flask-sqlalchemy query datetime intervals

限于喜欢 提交于 2019-12-06 03:49:58
I have defined a table with flask-sqlalchemy. Displayed below. class Notes(db.Model): id = db.Column(db.Integer, primary_key=True) notes = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) added_at = db.Column(db.DateTime, default=db.func.now()) @staticmethod def newest(num): return Notes.query.order_by(desc(Notes.added_at)).limit(num) I'm attempting to write a query that is to replace and already existing direct query, which looks like this. select notes,user,added_at from notes where added_at >= now() - INTERVAL 8 HOUR; However based

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

和自甴很熟 提交于 2019-12-06 03:22:47
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 I figured out the problem. The issue was sometimes database connection was going to lost state, which is causing pool size to be Exhausted

Ordering by subquery in SQLAlchemy

橙三吉。 提交于 2019-12-06 03:05:53
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(scalar.desc()).all() But it seems that I don't understand how scalars work. Reading docs for the 5th time