flask-sqlalchemy

TypeError: 'dict' object does not support indexing thrown on second instance of this query

孤者浪人 提交于 2020-01-14 09:52:06
问题 So I am building a query based on user input in flask with this code: if empty_indic_form.validate_on_submit(): query='select name, year, value, display_name from literal inner join ent on ent_id=ent.id where display_name in (' for i in indic_form.indicators.data: query=query+'\''+i+'\',' query=query[:-1]+') ' query=query+'and name in (' for c in order_c: query=query+c+',' query=query[:-1]+')' data_return=db.engine.execute(query).fetchall() I have confirmed that query looks like what it is

SQLAlchemy func.count with filter

半城伤御伤魂 提交于 2020-01-14 09:16:10
问题 I'm using a framework that does pagination like this: def get_count_query(self): return self.session.query(func.count('*')).select_from(self.model) def paginate(self): ... <irrelevant>... count = self.get_count_query.scalar() ... I want to override the get_count_query method to use my own query because I'm filtering some results and get_count_query just returns all elements in the table. Queries are created dynamically, for example one query could be: Asset.query.join(StatusLabel).filter

How does `nullable=False` work in SQLAlchemy

人盡茶涼 提交于 2020-01-14 07:22:27
问题 From SQLAlchemy docs: nullable – If set to the default of True, indicates the column will be rendered as allowing NULL, else it’s rendered as NOT NULL. This parameter is only used when issuing CREATE TABLE statements. I thought setting nullable=True for a Column basically made that Column required. For example: class Location(db.Model): __tablename__ = 'locations' id = db.Column(db.Integer, primary_key=True) latitude = db.Column(db.String(50), nullable=False) ... However, when I create a

Why do I get a 'NameError' with this import?

隐身守侯 提交于 2020-01-14 05:30:18
问题 I'm building a web app that uses Flask and SQLAlchemy, but I can't seem to see the reason why this won't import correctly and work. I'm trying to test my database that I'm building, but I keep getting a NameError : File1: from flask.ext.sqlalchemy import SQLAlchemy from flask import Flask from File2 import db, data1, data2, data3 File2: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///DB_File.db' #

Why do I get a 'NameError' with this import?

怎甘沉沦 提交于 2020-01-14 05:30:13
问题 I'm building a web app that uses Flask and SQLAlchemy, but I can't seem to see the reason why this won't import correctly and work. I'm trying to test my database that I'm building, but I keep getting a NameError : File1: from flask.ext.sqlalchemy import SQLAlchemy from flask import Flask from File2 import db, data1, data2, data3 File2: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///DB_File.db' #

How to paginate in Flask-SQLAlchemy for db.session joined queries?

倾然丶 夕夏残阳落幕 提交于 2020-01-12 07:27:23
问题 Say, we have the following relationships: a person can have many email addresses a email service provider can (obviously) serve multiple email address So, it's a many to many relationship. I have three tables: emails, providers, and users. Emails have two foreign ids for provider and user. Now, given a specific person, I want to print all the email providers and the email address it hosts for this person, if it exists. (If the person do not have an email at Gmail, I still want Gmail be in the

Flask: are blueprints necessary for app factories?

二次信任 提交于 2020-01-12 03:14:09
问题 I want to have several app factories(currently: one for development, and another one for testing). I am wondering what is the proper way to implement them. Currently I use app object to register views(via @app.route() decorator). Do I need to start using blueprints(instead of app) to register views? Is there any way to have proper app factories without blueprients? 回答1: technically, you don't need blueprints, you can just register each route on your create_app function. Generally speaking

Flask-SQLAlchemy: sqlite3 IntegrityError

Deadly 提交于 2020-01-11 07:55:07
问题 I'm creating an application to replace current tab managers in the browser. I've created a simple one-to-many relationship between groups - table Topic, and tabs - table Tab. I want to be able to automatically delete topic's children if I delete it. This is what I currently have: from flask import request, redirect, url_for, render_template, Flask from flask_sqlalchemy import SQLAlchemy from sqlalchemy import exc from flask_whooshee import Whooshee from datetime import datetime app = Flask(_

Flask-SQLAlchemy. Create several tables with all fields identical

旧时模样 提交于 2020-01-11 07:25:06
问题 I'm using Flask with its SQLAlchemy extension. I need to define several model classes, which will create tables in MySQL database. The tables will only differ by name, all the field names/datatypes in them will be identical. How do I define the classes for all those tables? I'm thinking of some inheritance, but I'm not quite sure how exactly would I do that. 回答1: Just define all your columns in a mix-in class: from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()

Flask-SQLAlchemy. Create several tables with all fields identical

做~自己de王妃 提交于 2020-01-11 07:24:48
问题 I'm using Flask with its SQLAlchemy extension. I need to define several model classes, which will create tables in MySQL database. The tables will only differ by name, all the field names/datatypes in them will be identical. How do I define the classes for all those tables? I'm thinking of some inheritance, but I'm not quite sure how exactly would I do that. 回答1: Just define all your columns in a mix-in class: from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()