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

Pydantic root_validator unexpected behaviour

I have a file test.py like this: from pydantic import root_validator from sqlmodel import SQLModel from typing import List from devtools import debug class Base(SQLModel): @root_validator(pre=True) def validate(cls, values): …
chc
  • 498
  • 1
  • 4
  • 18
1
vote
0 answers

How to avoid creating multiple sessions when using FastAPI Dependencies with Security?

I am using a synchronous (SQLite) session in a FastAPI Dependency as shown in the SQLModel Tutorial: def get_session(): with Session(engine) as session: yield session However, when a route "depends" on a Dependency and a Security that…
DurandA
  • 1,095
  • 1
  • 17
  • 35
1
vote
1 answer

How to implement a "Citation" table? (using SQLModel or SQLAlchemy)

I'm struggling with implementing the concept of "scientific paper citation" in SQL. I have a table of Papers. Each Paper can cite many other Papers and, vice-versa, it can be cited by many other more. Here's the code I wrote class Paper(SQLModel,…
chc
  • 498
  • 1
  • 4
  • 18
1
vote
1 answer

'__post_init__' or dynamically create new fields of SQLModel after initiation

My objective is to create a new field based on another field after a Request is posted to FastAPI. My attempt was: import functools from datetime import date, datetime from slugify import slugify from sqlmodel import Field, SQLModel class…
Jim
  • 450
  • 2
  • 10
1
vote
1 answer

How should we manage datetime fields in SQLModel in python?

Let's say I want to create an API with a Hero SQLModel, below are minimum viable codes illustrating this: from typing import Optional from sqlmodel import Field, Relationship, SQLModel from datetime import datetime from sqlalchemy import Column,…
Jim
  • 450
  • 2
  • 10
1
vote
1 answer

How to define a CheckConstraint for an integer field in SQLModel

Assume we have such atable. How we can define a CheckConstraint for age? from sqlmodel import SQLModel, Field, Column, String, Integer from typing import Optional class Hero(SQLModel, table=True): __tablename__ = "heroes" id: Optional[int]…
haku
  • 35
  • 5
1
vote
1 answer

Use PostGIS geometry types in SQLModel

Is it possible to use PostGIS geometry types in models created in SQLModel? If yes, how can it be done?
Charalamm
  • 1,547
  • 1
  • 11
  • 27
1
vote
3 answers

Clean architecture principles in FastAPI + SQLModel development

When I develop with Flask and SQLAlchemy, I commonly use the following layers: A repository layer, that deals with database connections and data persistence; A use cases layers, a bunch of functions that use the repository and do exactly what the…
André Carvalho
  • 111
  • 1
  • 11
1
vote
0 answers

Query an instrumentedattribute by its length in SQLAlchemy

The way the database is setup is like this. I'm using SQLModel, but underneath it's SQLAlchemy. class Paper(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) similar: List["PaperSimilarLink"] =…
phil0s0pher
  • 525
  • 10
  • 21
1
vote
3 answers

Missing cluster identifier Error when connecting to serverless cockroachdb using the Python SQLmodel library

I have a python app which is using the SQLmodel library as an ORM wrapper. After successfully creating a database and multiple tables with sqlite3, I like to switch to cockroach db. Therefore I changed the connection string in the create_engine…
comboner
  • 11
  • 3
1
vote
1 answer

getting joined tables from sqlmodel as a nested responde model in fastapi

I cannot figure it out how to display a one to many relationship using fastapi and sqlmodel. I've read through this question but my case seems to be slightly different. Specially in the function call. This is my schemas.py: from typing import…
moth
  • 1,833
  • 12
  • 29
1
vote
1 answer

SQLMODEL background error with date object

I'm trying to compare date objects using sqlmodel,fastapi,sqlalchemy. My ORM class looks like that: class Evergreen(SQLModel,table=True): id_seq: Optional[int] = Field(default=None,primary_key=True) phase_end: Optional[date] = None …
moth
  • 1,833
  • 12
  • 29
1
vote
2 answers

How to dynamically define an SQLModel class

Context SQLModel is heavily based on Pydantic. The latter has the create_model function allowing you to create/define a model class at runtime, by passing the field definitions as arbitrary keyword arguments. There seems to be no special version of…
Daniil Fajnberg
  • 12,753
  • 2
  • 10
  • 41
1
vote
1 answer

How to import a Pydantic model into SQLModel?

I generated a Pydantic model and would like to import it into SQLModel. Since said model does not inherit from the SQLModel class, it is not registered in the metadata which is why SQLModel.metadata.create_all(engine) just ignores it. In this…
inviridi
  • 13
  • 2
1
vote
0 answers

How to convert SQLAlchemy data model into SQLModel data model

Context: I want to build an API using FastAPI and SQLModel I need to use database reflection to create the SQLModel table models based on existing database I couldnt find how to do database reflection directly in SQLModel So I do database…
Toffy_
  • 11
  • 1