flask-sqlalchemy

Flask-SQLAlchemy with_for_update() row lock

限于喜欢 提交于 2020-04-10 03:49:11
问题 I have a model called 'User', and 'User' has 'Money'. There a scenario that multiple session can read the model 'User' and update 'money' at the same time. Session 2 should read the 'money' value after session 1 updated successfully. I tried to lock the 'User' row when updating. Here's my code. user = User.query.with_for_update().filter_by(id=userid).first() print('000000') before_money = user.money print('111111') time.sleep(1) user.money -= 0.1 print('User:' + str(user.id) + '***' + str

Flask, SQLAlchemy : KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

偶尔善良 提交于 2020-04-02 16:44:25
问题 I am trying to follow the instructions from the following tutorial: Tutorial I downloaded the code from the following repo: Repo However when I run it locally and try to add something to the database, I get the following error: builtins.KeyError KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS' When I tried to read the traceback, I realised that even if I add a variable SQLALCHEMY_TRACK_MODIFICATIONS to the config file, some python library file is unable to recognise it exists. Looks like there is

Flask, SQLAlchemy : KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

橙三吉。 提交于 2020-04-02 16:42:04
问题 I am trying to follow the instructions from the following tutorial: Tutorial I downloaded the code from the following repo: Repo However when I run it locally and try to add something to the database, I get the following error: builtins.KeyError KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS' When I tried to read the traceback, I realised that even if I add a variable SQLALCHEMY_TRACK_MODIFICATIONS to the config file, some python library file is unable to recognise it exists. Looks like there is

How to clone a sqlalchemy db object with new primary key

匆匆过客 提交于 2020-03-17 09:53:31
问题 I want to clone a sqlalchemy object: I tried it by product_obj = products.all()[0] #here products is service name product_obj.product_uid = 'soemthing' #here product_uid is the pk of product model products.save(product_obj) it is just updating the old_object only here is the code of products.save function class Service(object): __model__ = None def save(self, model): self._isinstance(model) db.session.add(model) db.session.commit() return model 回答1: This should work: product_obj = products

How to use avg and sum in SQLAlchemy query

送分小仙女□ 提交于 2020-03-17 05:04:21
问题 I'm trying to return a totals/averages row from my dataset which contains the SUM of certain fields and the AVG of others. I could do this in SQL via: SELECT SUM(field1) as SumFld, AVG(field2) as AvgFld FROM Rating WHERE url=[url_string] My attempt to translate this into SQLAlchemy is as follows: totals = Rating.query(func.avg(Rating.field2)).filter(Rating.url==url_string.netloc) But this is erroring out with: TypeError: 'BaseQuery' object is not callable 回答1: You should use something like:

Using SQLAlchemy ORM for a non-primary key, unique, auto-incrementing id

北城以北 提交于 2020-03-14 09:39:27
问题 When I run the following code, I am expecting the first_name, and last_name to be a composite primary key and for the id to be an autoincrementing index for the row, but not to act as the primary key, as there the information in the rest of the table is what I need to define it's uniqueness, rather than the given ID. Base = declarative_base() Session = sessionmaker(bind=db) session = Session() class Person(Base): __tablename__ = "people" id = Column(Integer, index=True, unique=True,

网站后端.Flask.实战-社交博客开发-赋予角色?

人走茶凉 提交于 2020-03-12 19:51:12
1.用户在注册时,会被赋予适当的角色,管理员可通过FLASK_ADMIN全局变量中的邮件地址识别,其它用户使用默认角色 FlaskWeb/app/models.py #!/usr/bin/env python # -*- coding: utf-8 -*- """ # # Authors: limanman # OsChina: http://my.oschina.net/pydevops/ # Purpose: # """ from . import db, loginmanager from flask import current_app from flask_login import UserMixin from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from werkzeug.security import generate_password_hash, check_password_hash @loginmanager.user_loader def load_user(user_id): return User.query.get(int(user_id)) class Permission(object): FOLLOW = 0x01 COMMENT = 0x02

Error while importing module app using from Flask.app import db

痴心易碎 提交于 2020-03-04 18:56:26
问题 I'm new to flask, and have problem with importing db while running from python console/cmd. I'm facing this error : In[67]: os.getcwd() Out[67]: 'C:\\Users\\Desktop\\Python\\Flask' In[68]: os.listdir() Out[68]: ['app', 'app.db', 'config.py', 'Flask.py', 'migrations', 'venv', '__init__.py', '__pycache__'] In[69]: from Flask.app.models import User,Post Traceback (most recent call last): File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line

count the number of rows with a condition with SQLAlchemy

空扰寡人 提交于 2020-02-25 08:07:06
问题 I have a sqlite table like this. Which has 3 columns. I want to count the number of rows where user_id = 1 is this possible with SQLAlchemy? class UserImage(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) photo = db.Column(db.String(250)) This will return all the rows. How to modify this to get my expected result. rows = db.session.query(func.count(UserImage.user_id)).scalar() Thank you so much in advance. 回答1: You can do it

Flask SQLAlchemy does not close MySQL database connections

喜夏-厌秋 提交于 2020-02-25 04:29:06
问题 I have a Flask app using Flask-SQLAlchemy with a MySQL database where the db is defined as the following: db.py : from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() main.py : from db import db app = Flask(__name__) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://" + \ DB_USERNAME + ":" + DB_PASSWORD + "@" + DB_HOST + "/" + DB_DATABASE db.init_app(app) @app.teardown_appcontext def teardown_db(error): db.session.close() db