flask-sqlalchemy

Sqlalchemy filter by field in list but keep original order?

久未见 提交于 2019-12-04 17:35:56
问题 I have a Shoe model like this: class Shoe(db.Model): id = db.Column(db.Integer, primary_key = True) asin = db.Column(db.String(20), index = True) I have a list of ids like ids = [2,1,3] and when I query on the Shoe model such that the results have ids in the 'ids' list, I want back: [{id:2, asin:"111"},{id:1, asin:"113"},{id:3, asin:"42"}] but the problem is that using the following query statement doesn't preserve the original order, the results will come back random. How do I keep the order

sqlalchemy.orm.exc.UnmappedInstanceError in flask

旧巷老猫 提交于 2019-12-04 16:57:32
问题 I have been reading the SQLAlchemy docs, but I don't understand them. The error (UnmappedInstanceError) says something isn't mapped. What isn't mapped? I really don't get sqlalchemy and I want to go back to using naked sqlite, but so many people recommend this, so I thought I should learn it. Here is the traceback: File "C:\Users\Me\repos\mandj\venv\lib\site-packages\flask\app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "C:\Users\Me\repos\mandj\venv\lib\site

How do I properly set up flask-admin views with using an application factory?

怎甘沉沦 提交于 2019-12-04 16:34:18
I'm trying to setup flask-admin model views with SQLAlchemy against 'user' and 'role' models. Instead of a function admin view I'm getting: ValueError: Invalid model property name <class 'app.models.Role'>.desc Stack trace: Traceback (most recent call last): File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/manage.py", line 18, in <module> app = create_app(os.getenv('APP_CONFIG') or 'default') File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/app/__init__.py", line 49, in create_app admin.add_view(RoleAdmin(Role, db.session)) File "/usr/local/share/anaconda/envs/flaskbase27/lib

Flask unittest and sqlalchemy using all connections

限于喜欢 提交于 2019-12-04 13:24:33
问题 I've just run into an issue running unittests on my flask app after I had roughly 100 unittests. All unittests will pass, but when run all at once they will fail with the following error: OperationalError: (OperationalError) FATAL: remaining connection slots are reserved for non-replication superuser connections Everything is running in a virtualbox/vagrant/ubuntu12.04 instance on local machine. My postgres max_connections is set to 100 so I'm assuming that the connections aren't closing and

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

纵饮孤独 提交于 2019-12-04 11:34:10
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 statement."....in other words, it doesn't return the number of rows affected by the update or delete

Reset id count after table delete()

时光总嘲笑我的痴心妄想 提交于 2019-12-04 10:16:48
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 not cleared at all now config.py SQLALCHEMY_DATABASE_URI = 'postgresql://postgres:myPassword@localhost

flask_dance: Cannot get OAuth token without an associated user

China☆狼群 提交于 2019-12-04 09:46:41
I want to migrate flask_dance with my application to make the user authorize using google and another social networks. I am getting this error: Cannot get OAuth token without an associated user Before i do the connection between the blueprint and sqlalchemy backend, the application worked just fine, if i removed the google_blueprint.backend line the error disappear. Here is my __init__.py : import os from flask import Flask, redirect, url_for, current_app from flask_login import current_user from develop.models import ( db, User, OAuth ) from flask_dance.contrib.google import make_google

flask sqlalchemy example around existing database

纵饮孤独 提交于 2019-12-04 09:43:40
Problem: there needs to be full working example of auto-mapping sqlalchemy to an existing database in an app with multiple binds. I want to bind to two databases and have one auto-map the tables. I need to do this because i don't have control over one DB, thus would have to constantly re-write my models and might delete new tables every time i migrate. I love the answers given here , but i can't seem to make it work in Flask (I am able to use the sqlalchemy only to query as per the example). The model.py I set up from the example above results in EDIT I pulled the line db.Model.metadata

Using different binds in the same class in Flask-SQLAlchemy

帅比萌擦擦* 提交于 2019-12-04 09:32:39
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 easily? I've tried inheriting from a table class and then defining the bind there, and while that works, it

How to specify table relationships in SQLAlchemy with multi-level/multiple joins?

不想你离开。 提交于 2019-12-04 09:13:19
问题 I'm trying to define a relationship between two tables whose relations are indirect (i.e. through two other tables). The results I'm looking for can be fetched with this query: (db.session.query(Telnum) .filter(Account.customer==customer) .filter(Account.account_id == Subscription.account_id) .filter(Telnum.sub_id == Subscription.id) .order_by(Telnum.telnum) .all() ) where customer is a Customer object. I'm struggling to figure out how this would be defined as a relationship, similar to the