flask-sqlalchemy

Flask unit tests with SQLAlchemy and PostgreSQL exhausts db connections

随声附和 提交于 2019-12-10 21:33:34
问题 I'm running a suite of fairly straightforward test cases using Flask, SQLAlchemy, and PostgreSQL. Using an application factory, I've defined a base unit test class like so: class BaseTestCase(unittest.TestCase): def setUp(self): self.app = create_app() self.app.config.from_object('app.config.test') self.api_base = '/api/v1' self.ctx = self.app.test_request_context() self.ctx.push() self.client = self.app.test_client() db.create_all() def tearDown(self): db.session.remove() db.drop_all(app

Caching Flask-Login user_loader

笑着哭i 提交于 2019-12-10 21:06:19
问题 I had this. @login_manager.user_loader def load_user(id=None): return User.query.get(id) It was working fine until I introduced Flask-Principal. @identity_loaded.connect_via(app) def on_identity_loaded(sender, identity): # Set the identity user object identity.user = current_user # return if hasattr(current_user, 'id'): identity.provides.add(UserNeed(current_user.id)) # Assuming the User model has a list of roles, update the # identity with the roles that the user provides if hasattr(current

Flask-Admin with additional field in relationship Many to Many

*爱你&永不变心* 提交于 2019-12-10 18:19:07
问题 I have two tables: "Product", "Ingredient" and "ProductIngredient" class ProductIngredient(db.Model): __tablename__ = "product_ingredient" id = db.Column(db.Integer(), primary_key=True) product_id = db.Column('product_id', db.Integer, db.ForeignKey('product.id')) ingredient_id = db.Column('ingredient_id', db.Integer, db.ForeignKey('ingredient.id')) amount = db.Column(db.DECIMAL(10, 3)) class Ingredient(db.Model): __tablename__ = "ingredient" id = db.Column(db.Integer, primary_key=True) name =

Flask-SQLAlchemy multiple databases and binds

∥☆過路亽.° 提交于 2019-12-10 18:17:33
问题 URI: SQLALCHEMY_DATABASE_URI = "mssql+pymssql://user:password123@127.0.0.1/DbOne" SQLALCHEMY_BINDS = { "sql_server": "mysql+pymysql://user:password123@127.0.0.1/DbTwo" } Models.py class CrimMappings(db.Model): __tablename__ = "crim_mappings" id = db.Column(db.Integer, primary_key=True) date_mapped = db.Column(db.Date) county_id = db.Column(db.Integer) state = db.Column(db.String(20)) county = db.Column(db.String(100)) AgentID = db.Column(db.String(100), unique=True) CollectionID = db.Column

attributeError: can't set attribute with flask-SQLAlchemy [duplicate]

与世无争的帅哥 提交于 2019-12-10 16:54:47
问题 This question already has an answer here : Can't set attribute on result objects in SQLAlchemy flask (1 answer) Closed 5 months ago . I'm using Flask-SQLAlchemy version 2.1 which installs sqlalchemy version 1.x. My below code which first fetch an array of resultset and then loop over to modify an existing attribute used to work but now it does not. question_topic = Question.query.join(Topic).join(User,User.id==Question.user_id).add_columns(User.email,Question.question, Question.date, Topic

SqlAlchemy(Flask+Postgres) : How to update only a specific attribute of a json field?

孤街醉人 提交于 2019-12-10 16:05:25
问题 I have a table that has one column declared as a json and I need to update records by adding a key-value to the json value. model class User(db.Model): __tablename__ = 'users' loginId = db.Column(db.String(128), nullable=False, primary_key=True) _password = db.Column(db.String(128), nullable=True) views = db.Column(JSON, nullable=True) controller @mod_event.route('/view', methods=['POST']) def view(): try: params = request.json loginId = params['dream']['loginId'] users.update().\ where(users

Can I use SQLAlchemy relationships in ORM event callbacks? Always get None

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 15:04:58
问题 I have a User model that resembles the following: class User(db.Model): id = db.Column(db.BigInteger, primary_key=True) account_id = db.Column(db.BigInteger, db.ForeignKey('account.id')) account = db.relationship('Account', backref=db.backref('ref_users', cascade='delete')) ... def after_user_write(mapper, connection, target): target.account.invalidate_cache() event.listen(User, 'after_insert', after_user_write) event.listen(User, 'after_update', after_user_write) event.listen(User, 'after

Flask-SQLAlchemy check if database server is responsive

蓝咒 提交于 2019-12-10 14:22:40
问题 I am using flask-SQLAlchemy for my webservice. I would like to have an endpoint that checks status of the utilized MySQL database availability/responsiveness. How would I go about it? Thanks. Here are relevant pieces of my code: mywebsvc.py ... app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://mylogin:mypw@localhost/mydb' db.init_app(app) ... models_shared.py from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() models.py from models_shared import db class Car(db

Boolean field query with sqlalchemy

故事扮演 提交于 2019-12-10 13:32:33
问题 Postgres model: class Song(db.Model): id3_parsed = db.Column(db.Boolean, server_default=u'false') Running the following query gives the correct count: select count(*) from song where id3_parsed is false; But how do I do it with flask-sqlalchemy? This doesn't work: songs = Song.query.filter(Song.id3_parsed == False).all() 回答1: songs = Song.query.filter(Song.id3_parsed.is_(False)).all() 来源: https://stackoverflow.com/questions/31811119/boolean-field-query-with-sqlalchemy

“stale association proxy, parent object has gone out of scope” with Flask-SQLAlchemy

*爱你&永不变心* 提交于 2019-12-10 13:28:46
问题 I've actually never encountered this error before: sqlalchemy.exc.InvalidRequestError: stale association proxy, parent object has gone out of scope After doing some research, it looks like its because the parent object is being garbage collected while the association proxy is working. Fantastic. However, I'm not sure where it's happening. Relevant code: # models.py class Artist(db.Model): # ... tags = association_proxy('_tags', 'tag', creator=lambda t: ArtistTag(tag=t)) # ... class Tag(db