flask-sqlalchemy

SQLAlchemy inheritance not working

荒凉一梦 提交于 2019-12-03 12:12:48
I'm using Flask and SQLAlchemy. I have used my own abstract base class and inheritance. When I try to use my models in the python shell I get the following error: >>> from schedule.models import Task Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/teelf/projects/schedule/server/schedule/models.py", line 14, in <module> class User(Base): File "/home/teelf/projects/schedule/server/venv/lib/python3.4/site-packages/flask_sqlalchemy/__init__.py", line 536, in __init__ DeclarativeMeta.__init__(self, name, bases, d) File "/home/teelf/projects/schedule/server/venv

What is wrong with my relationships in SQL Alchemy?

为君一笑 提交于 2019-12-03 11:37:46
问题 I am using SQLAlchemy with Flask to create relationships for my application. I recently rewrote the relationships, and, no matter what I change, I keep getting the error: sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship CurriculumVersion.enrollments - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression. On

Sqlalchemy filter by field in list but keep original order?

最后都变了- 提交于 2019-12-03 11:08:32
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 of the list I filtered by? Incorrect one: Shoe.query.filter(Shoe.id.in_(my_list_of_ids)).all() Rob If

sqlalchemy postgresql enum does not create type on db migrate

雨燕双飞 提交于 2019-12-03 10:46:07
问题 I develop a web-app using Flask under Python3. I have a problem with postgresql enum type on db migrate/upgrade. I added a column "status" to model: class Banner(db.Model): ... status = db.Column(db.Enum('active', 'inactive', 'archive', name='banner_status')) ... Generated migration by python manage.py db migrate is: from alembic import op import sqlalchemy as sa def upgrade(): op.add_column('banner', sa.Column('status', sa.Enum('active', 'inactive', 'archive', name='banner_status'), nullable

Flask-SQLAlchemy - on the fly connections to multiple databases

断了今生、忘了曾经 提交于 2019-12-03 10:14:49
问题 I have a flask webapp where users will be able to connect to their own mysql databases and query their own tables What's the best way to create multiple connections (to different databases) using flask-sqlalchemy. It seems like it needs to be done with scoped_session and sessionmaker but cant seem to wrap my head around it. Also the second part of the question, once I create a connection to a mysql db for one of the users, how do i persist the connection across requests ? Currently, i put the

sqlalchemy.orm.exc.UnmappedInstanceError in flask

狂风中的少年 提交于 2019-12-03 10:10:41
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-packages\flask\app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e))

Alembic autogenerates empty Flask-SQLAlchemy migrations

怎甘沉沦 提交于 2019-12-03 10:03:26
问题 I'm using Alembic to handle migrations for Flask. alembic revision --autogenerate should, in theory, autogenerate a migration based on changes in my database. However, Alembic is simply generating a blank migration with the above command. There's a question very similar to this one, where the issue was that the proper models weren't being imported. However, I have imported the models from my Flask app, as shown in env.py : ... # import settings from Flask alembic_config = config.get_section

How to three-way many-to-many relationship in flask-sqlalchemy

回眸只為那壹抹淺笑 提交于 2019-12-03 09:40:29
问题 What's the proper way to design a three-way many-to-many in flask-sqlalchemy? Assume I have users, teams and roles. Users are assigned to teams. When assigned to a team, the user is also assigned a role within that team. from myapp import db class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), unique=True) def __init__(self, name): self.name = name def __repr__(self): return "<User(%s)>" % self.name class Team(db.Model): id = db.Column(db.Integer

sqlalchemy, select using reverse-inclusive (not in) list of child column values

怎甘沉沦 提交于 2019-12-03 08:35:21
问题 I have a typical Post / Tags (many tags associated with one post) relationship in flask-sqlalchemy, and I want to select posts which aren't tagged with any tag in a list I provide. First, the models I set up: class Post(db.Model): id = db.Column(db.Integer, primary_key=True) tags = db.relationship('Tag', lazy='dynamic') class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text(50)) post_id = db.Column(db.Integer, db.ForeignKey('post.id')) Something like db

Flask unittest and sqlalchemy using all connections

橙三吉。 提交于 2019-12-03 08:35:18
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 after running 100 tests I use up all the available ones. This person Flask unit tests with SQLAlchemy