flask-sqlalchemy

SQLAlchemy | Limit join result to one row for one-to-many relationship

99封情书 提交于 2020-01-05 04:08:12
问题 I have two entities: Projects and Student Lists. One Project can have many Student lists. I am attempting to join student list on project and only return the first row for each project based on custom ordering of student lists. Attempted Subquery: _whens = {ProjectStatus.APPROVED: 1, ProjectStatus.REJECTED: 2, ProjectStatus.SUBMITTED: 3, None: 4} sort_order = case(value=StudentList.student_list_status_id, whens=_whens) return self._session.query(StudentList). filter(StudentList.student_list

Unhashable Type error when modifying SqlAlchemy Models

元气小坏坏 提交于 2020-01-04 17:33:32
问题 I am using the most basic flask app practically copied from documentations, and am receiving an extremely annoying error. I've been able to trace it down but don't know how to solve it. I know the problem arises when implementing flask-security, but the error comes from inside sqlalchemy. Any advice? from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy import logging app = Flask(__name__) app.config['DEBUG'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' #

Unhashable Type error when modifying SqlAlchemy Models

牧云@^-^@ 提交于 2020-01-04 17:31:41
问题 I am using the most basic flask app practically copied from documentations, and am receiving an extremely annoying error. I've been able to trace it down but don't know how to solve it. I know the problem arises when implementing flask-security, but the error comes from inside sqlalchemy. Any advice? from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy import logging app = Flask(__name__) app.config['DEBUG'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' #

Count related items in a sqlalchemy model

送分小仙女□ 提交于 2020-01-04 09:16:08
问题 I'm trying to count the number of items in their respective categories and end up with a collection that I can iterate through in a jinja template. My final output is something like: category1, 5 category2, 10 category3, 0 The zero items case is important. My model is: class Category(Base): __tablename__ = 'category' id = Column(Integer, primary_key=True) name = Column(String(80), unique=True) user_id = Column(Integer, ForeignKey('user.id')) user = relationship(User) class Item(Base): _

Flask-security create role,user and linking user_id to role_id

这一生的挚爱 提交于 2020-01-04 02:54:59
问题 I using Flask-Security with SQLAlchemy When want to add user or role def addrole(): form=addroll() createRole=user_datastore.create_role(name=form.role.data,description=form.description.data) db.session.add(createRole) db.session.commit() in mysql table add one record belong my creation and two or tree blank record same think happen when i want to create user To associate a user with a role, I have the following : @app.route('/addR', methods=['GET', 'POST']) @login_required def addR(): email1

Proper way to prevent SQLAlchemy from re-running queries on expired objects?

别说谁变了你拦得住时间么 提交于 2020-01-03 20:49:43
问题 I'm having trouble wrapping my head around how to deal with expired sqlalchemy objects in a flask request. Let's say I do something like the following: from models import Foo, Bar @app.route("/page") def page(): foos = Foo.query.all() for foo in foos: b = Bar(foo.data) db.session.add(b) db.session.commit() return render_template('page.html', foos=foos) and then in page.html : {% for foo in foos %} {{ foo. name }} {% endfor %} SQLAlchemy will then do a select query for each foo in the template

Proper way to prevent SQLAlchemy from re-running queries on expired objects?

夙愿已清 提交于 2020-01-03 20:48:33
问题 I'm having trouble wrapping my head around how to deal with expired sqlalchemy objects in a flask request. Let's say I do something like the following: from models import Foo, Bar @app.route("/page") def page(): foos = Foo.query.all() for foo in foos: b = Bar(foo.data) db.session.add(b) db.session.commit() return render_template('page.html', foos=foos) and then in page.html : {% for foo in foos %} {{ foo. name }} {% endfor %} SQLAlchemy will then do a select query for each foo in the template

Sqlalchemy does not work with pagination

六眼飞鱼酱① 提交于 2020-01-03 16:50:14
问题 I am totally new with flask and sqlalchemy. I try to make one web site just to get better understanding of flask. There are plenty of tutorials and they are mostly use sqlite. For my purpose I use postgres. and I need one page where I can use pagination. But all the time I am getting the error AttributeError: 'Query' object has no attribute 'paginate' my database uri = os.environ.get('DATABASE_URL', 'postgres://login:password@127.0.0.1/db_name') engine = create_engine(uri, convert_unicode

Sqlalchemy does not work with pagination

落爺英雄遲暮 提交于 2020-01-03 16:50:10
问题 I am totally new with flask and sqlalchemy. I try to make one web site just to get better understanding of flask. There are plenty of tutorials and they are mostly use sqlite. For my purpose I use postgres. and I need one page where I can use pagination. But all the time I am getting the error AttributeError: 'Query' object has no attribute 'paginate' my database uri = os.environ.get('DATABASE_URL', 'postgres://login:password@127.0.0.1/db_name') engine = create_engine(uri, convert_unicode

SQLAlchemy idle in transaction

随声附和 提交于 2020-01-03 09:12:34
问题 I have application writen Python 3.6, Flask and SQLAlchemy (PostgreSQL). I encountered problems with hanging idle in transaction connections in my db. It's probably because I don't commit nor rollback after select queries. I use default SQLALchemy configuration: db = SQLAlchemy() Sample endpoint that creates hanging connections: class Test(Resource): def get(self, pk): return User.query.get(pk).serialize() What's the way of handling such select queries? Should I select then commit? Or select