flask-sqlalchemy

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

好久不见. 提交于 2021-02-05 09:15:10
问题 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

Using Flask-SQLAlchemy without Flask

风流意气都作罢 提交于 2021-02-04 22:09:36
问题 I had a small web service built using Flask and Flask-SQLAlchemy that only held one model. I now want to use the same database, but with a command line app, so I'd like to drop the Flask dependency. My model looks like this: class IPEntry(db.Model): id = db.Column(db.Integer, primary_key=True) ip_address = db.Column(db.String(16), unique=True) first_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow ) last_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow )

Using Flask-SQLAlchemy without Flask

吃可爱长大的小学妹 提交于 2021-02-04 22:01:33
问题 I had a small web service built using Flask and Flask-SQLAlchemy that only held one model. I now want to use the same database, but with a command line app, so I'd like to drop the Flask dependency. My model looks like this: class IPEntry(db.Model): id = db.Column(db.Integer, primary_key=True) ip_address = db.Column(db.String(16), unique=True) first_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow ) last_seen = db.Column(db.DateTime(), default = datetime.datetime.utcnow )

How do I call a database function using SQLAlchemy in Flask?

非 Y 不嫁゛ 提交于 2021-02-04 15:13:28
问题 I want to call a function that I created in my PostgreSQL database. I've looked at the official SQLAlchemy documentation as well as several questions here on SO, but nobody seems to explain how to set up the function in SQLAlchemy. I did find this question, but am unsure how to compile the function as the answer suggests. Where does that code go? I get errors when I try to put this in both my view and model scripts. Edit 1 (8/11/2016) As per the community's requests and requirements, here are

How to keep a session open across separate requests with Flask-SQLAlchemy

瘦欲@ 提交于 2021-01-29 15:44:10
问题 I have a webapp that communicates with a postgres database via Flask-SQLAlchemy. The app successfully adds/updates individual images to the database. The calls to func1_add_image , func2_add_image are done via separate fetch request from javascript code. # in python db = sqlalchemy(session_options={'autocommit': False}) ... @bp.route('/api/v1_2/func1_add_image', methods=['POST']) def func1_add_image(): db.session.add(image1) db.session.commit() ... @bp.route('/api/v1_2/func2_add_image',

`flask db migrate` error

ぃ、小莉子 提交于 2021-01-29 10:47:26
问题 We are trying to run a flask db migrate and flask db upgrade which throws the following error: Usage: flask db upgrade [OPTIONS] [REVISION] Error: The file/path provided (C) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py We have added the app's directory to the PYTHONPATH environment variable but still get the error. Any help would be appreciated. Below is our __init__.py code. Are we missing something? import logging from

SQL Query to group records and return them as a list in a many-to-one relationship

不想你离开。 提交于 2021-01-29 08:46:00
问题 I'm having a bit of trouble with using SQLAlchemy to its full potential in a Python application using Flask, and understanding how many-to-one relationships work and how they can be utilized. I'm working with a database of songs and artists with a schema that looks like this: CREATE TABLE song ( id INTEGER PRIMARY KEY AUTO_INCREMENT, title VARCHAR(64), artist_id INTEGER ); CREATE TABLE artist ( id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(64) ); Or, through Python SQLAlchemy, it looks

Sqlalchemy.exc.OperationalError: (psycopg2.OperationalError)

梦想的初衷 提交于 2021-01-29 07:00:50
问题 My Flask app(postgresql db) is working fine in local. I pushed my code to server and there I tried to run.py db migrate , it throws me these errors Traceback (most recent call last): File "run.py", line 11, in <module> create_app().run() File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 412, in run result = self.handle(sys.argv[0], sys.argv[1:]) File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 383, in handle res = handle(*args, **config)

what could cause the FlaskSQLAlchemy session to change?

时光总嘲笑我的痴心妄想 提交于 2021-01-29 05:43:08
问题 We are using the Flask development server which I know we need to change. It seems that when two requests come in very close to each other bad things happen. On one of those occasions we got this error upon commit: sqlalchemy.exc.InvalidRequestError: Object '<SomeClass at 0x7fca30277b38>' is already attached to session '41' (this is '42') I know that there should be one FlaskSQLAlchemy session per request. Would this error imply that processing of the second request changed the session? What

One or more mappers failed to initialize. Could not locate any relevant foreign key columns for primary join condition

好久不见. 提交于 2021-01-29 05:26:47
问题 I have tried to implement this code from sqlalchemy import Column, ForeignKey, Integer, String, ForeignKeyConstraint from sqlalchemy_continuum import make_versioned , version_class, parent_class from sqlalchemy.ext.declarative import declarative_base import sqlalchemy as sa params = urllib.parse.quote_plus \ (r'Driver={ODBC Driver 17 for SQL Server};Server=HP;Database=Navigation;Uid=sa;Pwd=<added>;Encrypt=yes;TrustServerCertificate=yes;Connection Timeout=30;') conn_str = 'mssql+pyodbc:///