sqlalchemy

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

Trigger a mapper event in SQLAlchemy for a "soft delete" feature mixin class

Trigger a mapper event in SQLAlchemy for a "soft delete" feature mixin class Question: I’m implementing a soft delete mixin so that whenever I call Object.delete() it doesn’t actually delete it, but rather sets a deleted_at column to the current time. Even though nothing actually gets deleted, I would like the app to act as …

Total answers: 1

Generate SQLAlchemy models from Pydantic models

Generate SQLAlchemy models from Pydantic models Question: So I have this large JSON dataset with a huge number of properties (as well as nested objects). I have used datamodel-code-generator to build pydantic models. The JSON data has been imported into pydantic models (with nested models). Now I need to push them into the database. How …

Total answers: 1

How to return the average value using fastapi & pydantic

How to return the average value using fastapi & pydantic Question: I am new to fasts-api and python as I am a mobile developer. At the moment I managed to get this response from my API (please look at averageLevel which is an array at the moment): [ { "user_id": 139, "event_date": "2023-03-20T12:18:17", "public": 1, …

Total answers: 1