flask-sqlalchemy

Flask-SQLAlchemy database engine returns table names, but the table keys in metadata are empty

倾然丶 夕夏残阳落幕 提交于 2021-02-07 09:25:24
问题 I am connected to a MS SQL Server. The following returns all the table names in the database: app.config.from_object('config') db = SQLAlchemy(app) db.engine.table_names() However, this doesn't: db.metadata.tables.keys() // returns: dict_keys([]) Similarly, this doesn't work: db.table('dbo.users').primary_key // returns: ColumnSet([]) However, I am able to execute SQL queries. What would be the problem? 回答1: Engine.table_names gives you a list of available table names from the database.

How to render image from Flask-SQLAlchemy on page?

浪尽此生 提交于 2021-02-07 08:15:39
问题 I'm trying to render image on my website but it's doesn't work (i see only corrupted file icon). Could You please tell me what i'm doing wrong ? The way how I upload the image: @app.route('/UploadImages') def UploadImages(): return render_template('UploadImages.html') @app.route('/uploadImages', methods=['POST']) def uploadImages(): name = current_user.USERNAME file = request.files['inputFile'] newFile=IMAGES( NAME=file.filename, USERNAME=name, DATA=file.read() ) db.session.add(newFile) db

How to render image from Flask-SQLAlchemy on page?

我只是一个虾纸丫 提交于 2021-02-07 08:15:22
问题 I'm trying to render image on my website but it's doesn't work (i see only corrupted file icon). Could You please tell me what i'm doing wrong ? The way how I upload the image: @app.route('/UploadImages') def UploadImages(): return render_template('UploadImages.html') @app.route('/uploadImages', methods=['POST']) def uploadImages(): name = current_user.USERNAME file = request.files['inputFile'] newFile=IMAGES( NAME=file.filename, USERNAME=name, DATA=file.read() ) db.session.add(newFile) db

Rollback Many Transactions between tests in Flask

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-06 15:52:53
问题 My tests take a long time to run and I am trying to rollback transactions between tests instead of dropping and creating the tables between tests. The issues is that in some tests I do multiple commits. EDIT: How do I rollback transactions between tests so that tests will run faster Here is the Base class used for testing. import unittest from app import create_app from app.core import db from test_client import TestClient, TestResponse class TestBase(unittest.TestCase): def setUp(self): self

What is the difference between Session and db.session in SQLAlchemy?

限于喜欢 提交于 2021-02-06 08:50:49
问题 In the event mapper level docs it says that Session.add() is not supported, but when I tried to do db.session.add(some_object) inside after_insert event it worked, example: def after_insert_listener(mapper, connection, user): global_group = Group.query.filter_by(groupname='global').first() a = Association(user,global_group) db.session.add(a) event.listen(User, 'after_insert', after_insert_listener) Basically any new user should be part of global_group, so I added it in the after_insert event.

What is the difference between Session and db.session in SQLAlchemy?

我的未来我决定 提交于 2021-02-06 08:50:28
问题 In the event mapper level docs it says that Session.add() is not supported, but when I tried to do db.session.add(some_object) inside after_insert event it worked, example: def after_insert_listener(mapper, connection, user): global_group = Group.query.filter_by(groupname='global').first() a = Association(user,global_group) db.session.add(a) event.listen(User, 'after_insert', after_insert_listener) Basically any new user should be part of global_group, so I added it in the after_insert event.

Flask SQLAlchemy filter by value OR another

妖精的绣舞 提交于 2021-02-05 13:57:03
问题 I have a Flask project that interacts with MySQL db through Flask-SQLAlchemy . My question is, how to select a row from the database based on a value OR another value. The results I want in SQL looks like this SELECT id FROM users WHERE email=email OR name=name; How to achieve that in Flask-SQLAlchemy ? 回答1: The following may help: # app.py from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'url_or_path/to/database' db

Flask SQLAlchemy filter by value OR another

天大地大妈咪最大 提交于 2021-02-05 13:55:13
问题 I have a Flask project that interacts with MySQL db through Flask-SQLAlchemy . My question is, how to select a row from the database based on a value OR another value. The results I want in SQL looks like this SELECT id FROM users WHERE email=email OR name=name; How to achieve that in Flask-SQLAlchemy ? 回答1: The following may help: # app.py from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'url_or_path/to/database' db

Flask SQLAlchemy query with order_by returns error no such column

本小妞迷上赌 提交于 2021-02-05 12:09:50
问题 Here is my models: from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) class Person(db.Model): __tablename__ = 'persons' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), nullable=False, unique=True) pets = db.relationship('Pet', backref='owner', lazy='dynamic') def __init__(self, *args, **kwargs): super(Person, self).__init__(*args, **kwargs) def __repr__(self): return f'<Person id:{self.id} name:{self.name}>'

What's the difference between an association table and a regular table?

浪子不回头ぞ 提交于 2021-02-05 09:17:27
问题 I don't think I fully understand association tables. I know how to work with normal tables i.e add rows and what not but I don't understand how to work with an association table. why would I use the below student_identifier = db.Table('student_identifier', db.Column('class_id', db.Integer, db.ForeignKey('classes.class_id')), db.Column('user_id', db.Integer, db.ForeignKey('students.user_id')) ) Vs class studentIdent(db.model): db.Column(db.Integer, db.ForeignKey('classes.class_id')), db.Column