sqlalchemy

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

Pylint: func.max is not callable

Pylint: func.max is not callable Question: In my python code, I import func… from sqlalchemy.sql.expression import func Then, during my code I select data from a database table… select(func.max(MyTable.my_datetime)) …where my_datetime is a DateTime data type… from sqlalchemy.types import DateTime my_datetime = Column(‘my_datetime’, DateTime) The code runs OK, but in the vscode editor I am …

Total answers: 1

Filter on json data with in_() sqlalchemy

Filter on json data with in_() sqlalchemy Question: I want to filter features(Json field on database) with items = a or b, but here it returns 0, when I use other columns the filter works correctly. It returns correct data with ["a"] or ["b"] too, what is the reason? and what is the solution? data.filter(Data.id.in_([1,2])) …

Total answers: 2

Using `sqlalchemy.orm` with composite primary keys

Using `sqlalchemy.orm` with composite primary keys Question: Using sqlalchemy.orm I am trying to link two tables on a composite key, but keep getting an error. Unfortunately, the official docs provide an example that uses a single primary key (not composite), so I tried to come up with a basic example that reproduces the issue: from …

Total answers: 1

How to query over an attribute that consists of a string of JSON in SQLAlchemy?

How to query over an attribute that consists of a string of JSON in SQLAlchemy? Question: So, I table that looks like this. City id(Integer), name(String), metadata(String) and it consists of data like this 1, "London", "{‘lat’:51.5072, ‘lon’:0.1276, ‘Mayor’: ‘Sadiq Khan’, ‘participating’:True}", 2, "Liverpool", "{‘lat’:53.4084, ‘lon’:2.9916, ‘Mayor’: ‘Joanne Anderson’, ‘participating’:"False}, 3, "Manchester", "{‘lat’53.4808, ‘lon’:2.2426, ‘Mayor’: …

Total answers: 2

SQLAlchemy returns `list` instead of `Sequence[Row[_TP]]`

SQLAlchemy returns `list` instead of `Sequence[Row[_TP]]` Question: The type-hinting of the method sqlalchemy.engine.result.Result.fetchall() suggests the method is supposed to return a Sequence[Row[_TP]] but when I print print(type(result_1.fetchall())), I get a list. Is there anything I’m missing? My code is the following: if __name__ == "__main__": engine: sqlalchemy.engine.base.Engine = sqlalchemy.create_engine( "sqlite:///data_SQL/new_db.db", echo=False ) with engine.connect() as …

Total answers: 2

"Argument error" in Python using sqlalchemy

"Argument error" in Python using sqlalchemy Question: I’m trying to fetch data from database using sqlalchemy in but i get the following error sqlalchemy.exc.ArgumentError: Column expression, FROM clause, or other columns clause element expected, got [Table(‘params’, MetaData(), Column(‘paramID’, Integer(), table=, primary_key=True, nullable=False), Column(‘item’, String(), table=), Column(‘value’, Float(), table=), schema=None)]. Did you mean to say select(Table(‘params’, …

Total answers: 1

SQLAlchemy Mapper Events don't get triggered

SQLAlchemy Mapper Events don't get triggered Question: I can’t get Mapper Events to trigger in SqlAlchemy 2.0 tl;dr: I am able to perform operations but events never trigger. Below is the example they have in their docs: def my_before_insert_listener(mapper, connection, target): # execute a stored procedure upon INSERT, # apply the value to the row …

Total answers: 1