sqlalchemy

How to detect changes in custom types in sqlalchemy

How to detect changes in custom types in sqlalchemy Question: I am working on custom types in sqlalchemy columns using TypeDecorator. I’m storing my data in JSONB inside Postgres DB, but in code I am serializing and deserializing it in a data class. But when I change any field of that data class, it didn’t …

Total answers: 2

Flask application, db.create_all() does not create the database

Flask application, db.create_all() does not create the database Question: I’m new in creating Flask applications and I’m following a tutorial to quick start. I see that this question about db.create_all() is frequent, nonetheless I can’t understand why I can’t create a database. my project structure I’m initializing the dataset in __ init __.py as follows: …

Total answers: 1

Migrate PostgresDsn.build from pydentic v1 to pydantic v2

Migrate PostgresDsn.build from pydentic v1 to pydantic v2 Question: I have simple Config class from FastAPI tutorial. But it seems like it uses old pydantic version. I run my code with pydantic v2 version and get a several errors. I fix almost all of them, but the last one I cannot fix yet. This is …

Total answers: 5

Displaying database information in HTML using Flask, Jinja2, Python and SQLAlchemy

Displaying database information in HTML using Flask, Jinja2, Python and SQLAlchemy Question: I’m trying to display information that I tried inserting into a model on my Flask HTML site. I can’t figure out how to do this because the code doesn’t seem to display anything on the page. Here’s my model: class Book(db.Model): id = …

Total answers: 2

unexpected keyword argument in execute statement

unexpected keyword argument in execute statement Question: def add_application_to_db(job_id, data): with engine.connect() as conn: sql = text( "INSERT INTO application(id,job_id,fullname,email,linkedin,education,experience,resumeurl) VALUES (:job_id,:fullname,:email,:linkedin,:education,:experience,:resumeurl)" ) conn.execute( job_id=job_id, fullname=data["fullname"], email=data["email"], linkedin=data["linkedin"], education=data["education"], experience=data["experience"], resumeurl=data["resumeurl"], ) I am learning flask and using MySQL and sqlalchemy.I am trying to store info from user into the data base (table name = …

Total answers: 1

How to delete rows by a condition using sqlalmchemy and fastapi?

How to delete rows by a condition using sqlalmchemy and fastapi? Question: I’m trying to delete some rows that a ‘null’ in a Postgresql db by this: async def delete_empty_batches(): query = Batches.delete().where(Batches.c.acquired_by_warehouse_date is None) database.execute(query).fetchall() s = Batches.select() return await database.execute(s).fetchall() But nothing happens. I have tried to make a query in pgAdmin and …

Total answers: 2

Get table description from Python sqlalchemy connection object and table name as a string

Get table description from Python sqlalchemy connection object and table name as a string Question: Starting from a sqlalchemy connection object in Python and a table name as a string how do I get table properties, eg column names and datatypes. For example, connect to a database in sqlalchemy from sqlalchemy import create_engine conn = …

Total answers: 1

Get last actual data

Get last actual data Question: There is a table rule_chains: ID payroll_type_id stuff_department_id start_date 1 1 89 2023-03-01 2 2 89 2023-03-01 3 1 89 2023-04-01 My problem is to get the actual entity for today. For example today is 2023-03-01 and I need to get this two entity with id = 1 and id …

Total answers: 1

How to get Column from Table using Bracket Notation in declarative SQLAlchemy 2.0

How to get Column from Table using Bracket Notation in declarative SQLAlchemy 2.0 Question: I use python SQLAlchemy 2.0 ORM in declarative style to define my tables like such: from sqlalchemy.orm import DeclarativeBase from sqlalchemy import select class Example(DeclarativeBase): __tablename__ = ‘example’ foo = mapped_column(Text, nullable=False) I know that I can access table column in …

Total answers: 1

SQL query to create a nested pydantic structure

SQL query to create a nested pydantic structure Question: While working with SQLAlchemy 2.x and FastAPI, I ran into the problem of creating a nested structure for pydantic serialization. The specific essence of the problem lies in the implementation of the sql query. The fact is that the received data comes as a single tuple …

Total answers: 1