Skip to content
Advertisement

Tag: sqlalchemy

How to save a calculated column in SQLAlchemy?

I would like to know how to save a calculated value in a clean way in the database: An example (taken from the SQLAlchemy manual): So length is calculated. But for easier query look-ups (in another database tool), I would like to have this length saved as well into the table. So my first crude test was to simply add:

Flask SQLAlchemy reflection ignoring most of tables on Redshift

I’m creating the engine and the Metadata as follows Yields AttributeError: packages, and dir(Base.classes) returns no attributes with that name, neither with the orders name. Taking the only=[‘orders’, ‘packages’] off makes it reflect only a few random tables. Now, when using the inspector that comes with native SQLAlchemy the table actually works (link to documentation): Is this a bug, or

asyncpg.exceptions.DataError: invalid input for query argument $1: 217027642536 (value out of int32 range)

I’m working on a project that uses FastAPI alongside Pydantic and SQLAlchemy. I’m also using encode/databases to manage database connections. But for some weird reason, I get asyncpg.exceptions.DataError: invalid input for query argument $1: 217027642536 (value out of int32 range) anytime I try saving to the database. Here’s what my code looks like: database.py database_manager.py endpoints.py models.py services.py I can’t

Python flask-sqlalchemy: Do I have to commit session after a query?

I am writing an app in python flask-sqlalchemy with MySQL DB (https://flask-sqlalchemy.palletsprojects.com/en/2.x/) and I am wondering if I have to make “db.session.commit() or db.session.rollback()” after GET call, which only query DB . For example: Answer orders = Order.query.all() is a SELECT query that could be extended to include additional filters (WHERE etc.). It doesn’t alter the database, it simply reads

Delete sql table rows from python

I can successfully connect my python notebook with sql server this way : Let’s take this sample dataframe : I have a sql table with the same columns, named table_name. Objective : I want to delete in table_name all the rows where a row in df has the same Name, same Date, same Status, and Max = 0 (only in

List of object attributes in pydantic model

I use Fast API to create a web service. There are following sqlAlchemy models: Pydantic schemas are below: What I have now is: What I want to achieve is to get user from api in following structure: Is that possible? How should I change schemas to get this? Answer If you are okay with handling the how to “get user

Partial update in FastAPI

I want to implement a put or patch request in FastAPI that supports partial update. The official documentation is really confusing and I can’t figure out how to do the request. (I don’t know that items is in the documentation since my data will be passed with request’s body, not a hard-coded dict). How can I complete my code to

How to upsert pandas DataFrame to PostgreSQL table?

I’ve scraped some data from web sources and stored it all in a pandas DataFrame. Now, in order harness the powerful db tools afforded by SQLAlchemy, I want to convert said DataFrame into a Table() object and eventually upsert all data into a PostgreSQL table. If this is practical, what is a workable method of going about accomplishing this task?

Advertisement