flask-sqlalchemy

How to order by count of many-to-many relationship in SQLAlchemy?

无人久伴 提交于 2019-12-12 07:19:28
问题 I'd like to order by the count of a many-to-many relationship on a table. My starting point was the (excellent) response to this thread. However, in my case, there is a third table involved and I need my backrefs to be 'joined' rather than 'dynamic'. The combination of those two changes results in ProgrammingErrors when I try the solution to that question. Here's my setup. I have the same many-to-many relationship between User and Post. I also have a one-to-many relationship between Post and

Getting errors with passing parameters with fliter_by function in SQLAlchemy

六眼飞鱼酱① 提交于 2019-12-12 05:16:15
问题 I have the following class for a table in SQLalchemy class STDcodes(db.Model): id = db.Column(db.Integer, primary_key=True) stdcode = db.Column(db.Integer, nullable=False) city = db.Column(db.String(30), nullable=False) state = db.Column(db.String(30), nullable=False) def __init__(self, stdcode, city, state): self.stdcode = stdcode self.city = city self.state = state def __repr__(self): return '<City {}>'.format(self.city) Now I have a text file with some city names. I want to find the cities

Flask search query not using filters

早过忘川 提交于 2019-12-12 04:19:03
问题 I have a working Flask API and now I want to implement search queries. My understanding is that the filter is applied on the client and the Flask API takes care of it. Flask==0.10.1 Flask-HTTPAuth==2.7.0 Flask-Limiter==0.9.1 Flask-Login==0.3.2 Flask-Mail==0.9.1 Flask-Principal==0.4.0 Flask-Restless==0.17.0 I have followed documentation and performed my search query, but still just retrieving same results: http://flask-restless.readthedocs.org/en/latest/searchformat.html No filter: curl -u aaa

How to customize flask admin QuerySelectMultipleField choice?

我的梦境 提交于 2019-12-12 04:16:40
问题 I am using flask_admin to allow admin user to access database, where one-to-many relationship is presented. When editing an entry, I would like the dropdown menu to show only the options that meet the condition. I though query_factory can do this. The following is minimal example of what I have now: class OneSideObj(db.Model): id = db.Column(db.Integer, primary_key=True) active = db.Column(db.Boolean) many_side_obj_id = db.Column( db.Integer, db.ForeignKey('many_side_obj.id') ) class

TypeError: an integer is required, SQLAlchemy + Flask

纵然是瞬间 提交于 2019-12-12 04:14:44
问题 I've got the following code which runs on my machine without errors: def update_user(): user = User.query.get(session.get('user') or -1) if user is None: return if not user.token_valid(): session.pop('user', None) return user.lastlogin_time = int(time.time()) user.lastlogin_ip = request.remote_addr db.session.commit() However, when I deploy it to Linux VDS (Archlinux, same version of everything) I get the following error: Traceback (most recent call last): File "/usr/lib/python2.7/site

Getting sorted results in Flask-sqlalchemy

可紊 提交于 2019-12-12 03:36:44
问题 I have a MySQL database and below are the two Models defined. class Agent(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), nullable=False, unique=True) premium_date = db.Column(db.DateTime, nullable=True) class Property(db.Model): id = db.Column(db.Integer, primary_key=True) agent_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) agent = db.relationship('Agent') title = db.Column(db.String(255), nullable=False) I am getting all the

Flask-SQLAlchemy not creating my tables

浪尽此生 提交于 2019-12-12 03:25:23
问题 SQLAlchemy and for some reason when i run my create_db.py only the migration table is created. I tried it from python terminal with from modules import db,models then running db.create_all() but it still gives the same result. this is my models.py . from __init__ import db from datetime import datetime class Batch(db.Model): __tablename__='batch' batch_id = db.Column(db.String, primary_key=True) #total = db.Column(db.Integer) success = db.Column(db.Integer) failure = db.Column(db.Integer)

How do you create a table with Sqlalchemy within a transaction in Postgres?

与世无争的帅哥 提交于 2019-12-12 03:03:19
问题 I'm using Sqlalchemy in a multitenant Flask application and need to create tables on the fly when a new tenant is added. I've been using Table.create to create individual tables within a new Postgres schema (along with search_path modifications) and this works quite well. The limitation I've found is that the Table.create method blocks if there is anything pending in the current transaction. I have to commit the transaction right before the .create call or it will block. It doesn't appear to

SQLAlchemy distinct() doesn't return unique records with a keyword search

泄露秘密 提交于 2019-12-12 02:50:04
问题 I have a search bar which searches for users with the following query: db.session.query(User).filter(or_(User.name.contains(keyword), User.location.contains(keyword))).limit(16).all() As you can see, it searches using keywords (which comes from a list formed by the search query). If I search for 'John Smith', the search will find a user with a name containing 'John' and then the same user with a name containing 'Smith', so it will return the User twice. To solve this issue I tried using

Alembic Migrations for Flask

梦想与她 提交于 2019-12-11 19:24:54
问题 I have been trying to get the Alembic migration system configured for my Flask app for the past 7 hours with no success. Any ideas as to what I am doing wrong? I have tried all the solutions I found here, but they did not seem to work for me. I get the following error after running the commands: $ heroku run alembic revision -m "please work" --autogenerate Running `alembic revision -m please work --autogenerate` attached to terminal... up, run.6050 Traceback (most recent call last): File "