flask-sqlalchemy

Getting Flask-Migrate to Ignore SQL Views that are mapped as Flask-SQLAlchemy Models

荒凉一梦 提交于 2019-12-01 19:17:59
I am using Flask-SQLAlchemy to define my models, and then using Flask-Migrate to auto-generate migration scripts for deployment onto a PostgreSQL database. I have defined a number of SQL Views on the database that I use in my application like below. However, Flask-Migrate now generates a migration file for the view as it thinks it's a table. How do I correctly get Flask-Migrate / Alembic to ignore the view during autogenerate? SQL View name: vw_SampleView with two columns: id and rowcount . class ViewSampleView(db.Model): __tablename__ = 'vw_report_high_level_count' info = dict(is_view=True)

SQLAlchemy circular one-to-one relationship

落花浮王杯 提交于 2019-12-01 19:02:57
问题 I am trying to make a circular one-to-one relationship (not sure what the correct term is) with SQLAlchemy that looks the following: class Parent(Base): __tablename__ = 'parents' id = db.Column(Integer, primary_key=True) child_id = db.Column(db.Integer,db.ForeignKey("children.id", use_alter=True)) child = db.relationship("Child", uselist=False, foreign_keys=[child_id], post_update=True) class Child(Base): __tablename__ = 'children' id = db.Column(db.Integer, primary_key=True) parent_id = db

Create many to many on one table

霸气de小男生 提交于 2019-12-01 18:18:21
Flask-SQLAlchemy gives an example of how to create a many to many relationship. It is done between two different tables. Is it possible to create a many to many relationship on the same table? For example a sister can have many sisters, who would also have many sisters. I have tried: girl_sister_map = db.Table('girl_sister_map', db.Column('girl_id', db.Integer, db.ForeignKey('girl.id')), db.Column('sister_id', db.Integer, db.ForeignKey('girl.id'))) class Girl(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) sisters = db.relationship('Girl', secondary=girl

Minimizing the performance issues of loading a many to many relationship

こ雲淡風輕ζ 提交于 2019-12-01 17:27:30
I've been tokenizing an extremely large corpus. Each Unigram can occur in multiple Comments multiple times. I'm storing the Comment.ids in a list that is attached to the Unigram in the database every 250K newly counted unigrams or so. What I'm wondering is if there is a way to extend the comment id list--or a similar data structure--without querying and loading the existing list of comments tied to the Unigram (it can number in the the thousands). Or is there no way around the slow IO? Here is my model code: comments = db.Table('ngrams', db.Column('unigram_id', db.String, db.ForeignKey(

Flask-SQLAlchemy: sqlite3 IntegrityError

二次信任 提交于 2019-12-01 14:27:31
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(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' app.config['SQLALCHEMY_TRACK

Flask-SQLAlchemy. Create several tables with all fields identical

限于喜欢 提交于 2019-12-01 14:07:00
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. Just define all your columns in a mix-in class : from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class MyMixin(object): id = Column(Integer, primary_key=True) data = Column(String) class MyModel1(MyMixin,

Sqlalchemy - executing raw sql queries

社会主义新天地 提交于 2019-12-01 12:42:58
I'm using sqlalchemy in a flask application that connects to multiple databases, using binds as shown here . I want to execute a raw sql query on one of the non-primary databases. I'm trying to use session.execute , as shown here but it executes for the primary db. The API docs state that you can use a parameter: "bind – Optional Engine to be used as the bind". How do I access and specify the bind for the non-primary db and call session.execute to execute a query for that db? Alternately, is there any other way to go about it? Try this: from flask import current_app db.get_engine(current_app,

Can the contents of a relationship be filtered when querying parent model?

江枫思渺然 提交于 2019-12-01 12:06:36
I have two models representing movies and their show times. I would like to query for all movies, but their show_times relationship should only contain those show times that are in the future. class PKMovie(db.Model): id = db.Column(db.Integer, primary_key=True) url = db.Column(db.String(255)) show_times = db.relationship('ShowTime') class ShowTime(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.Date) pk_movie_id = db.Column(db.Integer, db.ForeignKey('pk_movie.id')) Is it possible to affect the contents of the relationship when querying the parent? davidism A

AttributeError: 'NoneType' object has no attribute 'time_recorded' in Flask, SQLAlchemy

感情迁移 提交于 2019-12-01 11:15:00
问题 I have an api endpoint that passes a variable which is used to make a call in the database. For some reason it cannot run the query yet the syntax is correct. My code is below. @app.route('/api/update/<lastqnid>') def check_new_entries(lastqnid): result = Trades.query.filter_by(id=lastqnid).first() new_entries = Trades.query.filter(Trades.time_recorded > result.time_recorded).all() The id field is: id = db.Column(db.String,default=lambda: str(uuid4().hex), primary_key=True) I have tried

Flask app getting error of “could not locate flask application. …FLASK_APP environment variable” for Flask Migrate

左心房为你撑大大i 提交于 2019-12-01 10:48:40
I have a file db_table.py that looks like: from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/mydb' db = SQLAlchemy(app) migrate = Migrate(app, db) .....db tables.... When I try to run: flask db init I get: Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable. I tried first manually setting FLASK_APP var, by doing set FLASK_APP=app.py then running flask db init again, but that didn't resolve the issue. The