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
1
vote
1 answer

How to use SQLModel with more than 1 database?

I have a FastAPI project using SQLModel as the orm. I would like to use multiple different databases in the same project. For example I would like one FastAPI endpoint to query 1 database and another FastAPI endpoint to query a completely…
configure.ng
  • 159
  • 1
  • 11
1
vote
1 answer

What is the best practice for defining an auto-incrementing primary key using Tiangolo's SqlModel library?

I am leveraging Tiangolo's SqlModel library for a FastAPI + PostgreSQL application. As best as I can tell, primary keys are defined as follows: class SomeSchema(SqlModel, table=True): __tablename__ = 'some_schema' id: Optional[int] =…
muad-dweeb
  • 995
  • 1
  • 9
  • 24
1
vote
1 answer

How do I construct a self-referential/recursive SQLModel

I want to define a model that has a self-referential (or recursive) foreign key using SQLModel. (This relationship pattern is also sometimes referred to as an adjacency list.) The pure SQLAlchemy implementation is described here in their…
1
vote
1 answer

Getting error TypeError: 'SQLModelMetaclass' object is not iterable

I have a fastapi project using SQLModel that has JWT auth active. I have a registration endpoint and am trying to return a subset of the user record when a post to the registration endpoint is successful. I seem to be able to return the 'User'…
configure.ng
  • 159
  • 1
  • 11
1
vote
1 answer

Return data from foreign key in FastAPI throws a greenlet error

I am trying to return connected data via foreign key in FastAPI but when I use a class in response_model that contains the data, the endpoint throws a greenlet error. I am using FastAPI and SQLModel (I know about the error with SQLModel…
Cheekou
  • 35
  • 6
1
vote
0 answers

How to perform a recursive query

I have a database of companies in a hierarchical structure where all the companies are in the same table but each refer to a parent company that is referenced in a parent_id column. This value matches their respective parent company's…
noob
  • 328
  • 2
  • 13
1
vote
1 answer

What does 'sa_relationship_kwargs={"lazy": "selectin"}' means on SQLModel with Fastapi?

I'm trying to use SQLModel with Fastapi, and on the way I found this example for implementing entities relationships, and I would like to know what does sa_relationship_kwargs={"lazy": "selectin"} means and what does it do? class…
0x55b1E06FF
  • 538
  • 1
  • 9
  • 24
1
vote
0 answers

Schema not show child list using SQLModel with FastAPI

I need help using SQLModel and FastAPI: from typing import Optional, List from sqlalchemy import UniqueConstraint from sqlmodel import Field, Relationship, SQLModel class Servers(SQLModel, table=True): id: Optional[int] = Field(default=None,…
Sr Momo
  • 11
  • 2
1
vote
0 answers

Different behavior Field in pydantic and SQLModel in OpenAPI spec

To my knowledge, SQLmodel is partially based on pydantic. Nevertheless, I get a different resulting OpenAPI specification when using the Field function from SQLmodel in FastAPI. For example, look at the following example: from typing import…
Ben
  • 1,519
  • 23
  • 39
1
vote
0 answers

How to query View in sqlmodel

from typing import Optional from sqlmodel import Field, Session, SQLModel, create_engine, select class HeroTeamView(SQLModel): name: str secret_name: str age: Optional[int] = None sqlite_file_name = "my.db" db_url =…
katch
  • 820
  • 1
  • 7
  • 24
1
vote
1 answer

SQLmodel and group_by

I recently completed the SQLmodel documentation, but it doesn't tell me how to use the group_by command. I am trying the following: SELECT reader_status, count(book_id) FROM readerbook WHERE reader_id=1 GROUP BY reader_id, reader_status to be used…
1
vote
2 answers

SQLModel - How to use only few fields from the base model

In the below example given here: https://sqlmodel.tiangolo.com/tutorial/create-db-and-table/ from sqlmodel import Field, SQLModel class Hero(SQLModel, table=True): id: int = Field(default=None, primary_key=True) name: str secret_name:…
Akash Ranjan
  • 922
  • 12
  • 24
0
votes
0 answers

Error fastapi.exceptions.ResponseValidationError and other relationship and back_population problems (not load related records)

Summary of my most important problem: Basically, I want to get my response validated by my schema from SQLModel, but I have the error and I don't have idea what field/part cause it, there is not an explanation. My minimum example code: # I had to…
Diego Gaona
  • 498
  • 6
  • 24
0
votes
0 answers

Inequality in SQLAlchemy query when using SQLModels/Pydantic model with UUID type column mappings (with AzureSQL)

I am creating a FastAPI application on top of an existing database (AzureSQL), the SQL table ID column (primary key) is populated by: CONVERT([varchar](36),newid()) My SQLModel class definition for the table is: from sqlmodel import Field,…
NixonInnes
  • 340
  • 1
  • 3
  • 10
0
votes
0 answers

Tree aggregation in python

I have 3 tables in Postgres. table -> ledger ledger is a tree structure. table -> leaf value of the tree leaf table -> result result of the tree aggregation I want a python code to connect to postgresql database and aggregate the tree from leaf…
navid sedigh
  • 279
  • 1
  • 5
  • 15