Questions tagged [sqlmodel]

Since SQL Model is a library that combines both SQL Alchemy and Pydantic, this tag is intended for only related questions to this library (SQLModel). Please be sure to tag SQL Alchemy and Pydantic related questions with the corresponding tags

SQLModel is a library for interacting with SQL databases from Python code, with Python objects.

It is designed to be intuitive, easy to use, highly compatible, and robust. SQLModel is based on Python type annotations and powered by Pydantic and SQLAlchemy.

171 questions
2
votes
1 answer

Getting nested (joined) tables to display in the OpenAPI interface provided by FastAPI and SQLModel

I'm having trouble understanding how to display the children data in a one-to-many relationship using FastAPI and SQLModel. I'm using Python 3.10.3, FastAPI version 0.78.0 and SQLModel version 0.0.6. Here's a simplified version of the parent/child…
writes_on
  • 1,735
  • 2
  • 22
  • 35
2
votes
1 answer

sqlmodel: filter by null

Referring to the filtering examples here: https://sqlmodel.tiangolo.com/tutorial/where/#filter-rows-using-where-with-sqlmodel, how do I fetch all heroes whose age is null. I need the equivalent of: select * from hero where age is null This…
Keerthi
  • 466
  • 6
  • 12
2
votes
1 answer

How to specify multiple "where" or "order_by" conditions?

It's not straight forward to find information on this so wondering if there are some docs I can look at but basically I want to achieve passing multiple conditions to either .where() or .order_by() that is safe from SQL injection. Here's how I am…
johojojoj
  • 43
  • 6
2
votes
1 answer

FastAPI / SQL MODEL / Create Multiple INSERTS

I am working in an API with FastAPI and SQLModel and like database PostgreSQL and I want to insert data like a bulk and I want to use the method bulk_save_objects but I got the following error…
CoDeC__
  • 93
  • 2
  • 9
2
votes
0 answers

INSERT OR IGNORE with SQLModel using Python

Does anyone know if its possible to get a INSERT OR IGNORE function with SQLModel? And if not, does anyone know if there is a workaround to get the same results? I haven't tried anything to get the results I'm look for as I'm fairly new to…
2
votes
1 answer

How do class declaration parameters work in python?

Recently I came across SQLModel package. It combines pydantic and sqlalchemy features to work with database objects. What I noticed in documentation is that table=True parameter that you use to declare a model class: from typing import…
Artem Ilin
  • 353
  • 2
  • 19
2
votes
1 answer

BigInteger with fastapi and sqlmodel

I'm using FastAPI with SQLModel which is based on pydantic, SQLAlchemy and type hints. And I'm trying to create a BitInteger (int64 is enough) column. How do I do that? My sql model declaration looks like that class ItemBase(sqlmodel.SQLModel): …
DarkSidds
  • 164
  • 11
2
votes
1 answer

alembic migrations with sqlmodel attempts to alter primary key colum

Given this model: from typing import Optional from sqlmodel import SQLModel, Field class SongBase(SQLModel): name: str artist: str = Field(index=False) #label: Optional[str] = Field(None, index=False) year: Optional[int] =…
Trondh
  • 3,221
  • 1
  • 25
  • 34
1
vote
0 answers

Make `TSTZRANGE` field in `SQLModel` model compatible with `FastAPI` response serialization

Consider the following model: from datetime import datetime from typing import Optional, Tuple from sqlalchemy.dialects.postgresql import TSTZRANGE from sqlalchemy import Column from sqlmodel import Field, SQLModel class DateRange(SQLModel): …
Pratik K.
  • 147
  • 2
  • 13
1
vote
1 answer

How to avoid SQL injection in JSONPATH with SQLAlchemy/SQLModel

I would like to avoid SQL injection in JSON path parts of queries with SQLModel. I haven't found a solution to provide parameters for the JSON path part in a safe way yet. Let's say we have a database access function like this one: from sqlmodel…
László Ács
  • 149
  • 1
  • 9
1
vote
1 answer

Unit Tests in FastAPI

I have a backend app developed with FastAPI, using SQLModel (SQLAlchemy & Pydantic) and connected to a Postgres database. I have integration tests to test if my endpoints are working fine with a stagging PG DB. But right now I have to write units…
FloCAD
  • 113
  • 3
  • 13
1
vote
1 answer

Tests with FastAPI and PostgreSQL

I'm developing a backend app with FastAPI connected to a PostgreSQL database and I'm a bit stuck and lost with the tests good practices. I read a lot stackoverflow posts and blogs but I'm not really used to backend development and I still doesn't…
FloCAD
  • 113
  • 3
  • 13
1
vote
1 answer

SqlAlchemy uses None/null as default value which conflicts with SqlServer

So I'm using sqlmodel (sqlalchemy with pedantic) and I have something like this model: class SequencedBaseModel(BaseModel): sequence_id: str = Field(alias="sequence_id") @declared_attr def sequence_id(cls): return Column( …
Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
1
vote
0 answers

Alembic revision treats tables as columns

I'm having problem with alembic revision. When running alembic revision --autogenerate, I do receive corrupted schema. Two tables out of 6 are there and there rest is treated as columns. I'm running out of ideas what's wrong. These are my…
kij89
  • 21
  • 2
1
vote
1 answer

Generate SQLAlchemy models from Pydantic models

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…
masroore
  • 9,668
  • 3
  • 23
  • 28
1 2
3
11 12