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
0
votes
0 answers

alembic to autogenerated computed columns , generated as always in postgres

I have a column as such in my model: class Mem(BaseMem,table=True): id: Optional[int] = Field(sa_column=Column(Integer,Identity(always=True,start=1,cycle=True),primary_key=True,nullable=False)) it_service_instance: Optional[str] =…
moth
  • 1,833
  • 12
  • 29
0
votes
1 answer

Query `MATERIALIZED VIEW` using declarative, class-based `SQLAlchemy` model definition

Given the following model schema definiton: from datetime import date, datetime, time from enum import Enum, unique from typing import Optional, Tuple from uuid import uuid4 from pydantic import UUID4, condecimal from sqlalchemy import Column,…
Pratik K.
  • 147
  • 2
  • 13
0
votes
0 answers

FastAPI and SQLModel to build a GraphQL API

I want to explore the creation of a graphql DB using Fastapi and Strawberry. I Start with the basic FastAPI API (taken from their website): from typing import List, Optional from fastapi import Depends, FastAPI, HTTPException, Query from sqlmodel…
gontxomde
  • 132
  • 9
0
votes
0 answers

subclasses with SQLModel in FastAPI

I'm writing classes for satellite components and need to specify dimensions. I've previously had the dimensions specified within the same class: from sqlmodel import Field from sqlmodel import SQLModel class PropulsionSystemInput(SQLModel): …
0
votes
0 answers

How to delete all rows matching a condition using SqlModel

The documentation of SqlModel about DELETE action: https://sqlmodel.tiangolo.com/tutorial/delete/ shows how to delete a single line using the functions .one() - If only a single record is expected or .first() - to get the 1st line from multiple…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
0
votes
1 answer

With SQLModel how can I send an record with uuid field and let it be autogenerated in Postgres

I am using SQLModel with FastAPI to insert data into Postgres. The model I am using is import uuid from datetime import datetime, time from typing import Optional from uuid import UUID from sqlmodel import Field, SQLModel, create_engine, Column,…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
0
votes
0 answers

FastAPI Authentication with Login/Group Roles of an existing Postgres DB

I've read a lot of the FastAPI documentation, Stackoverflow post and other blogs and none of them seems to talk about using the Login/Group Roles section (SELECT * FROM pg_role) of an existing Postgres Database for authentication purpose. I'm not…
FloCAD
  • 113
  • 3
  • 13
0
votes
0 answers

Supplying a manual foreign key as `sa_column` in sqlmodel

I want to have a foreign key on a child model that is set to null when the parent model is deleted (this is default behaviour in sqlmodel when the parent model has a backreference to the child model but I don't have that). In order to that one can…
user2268997
  • 1,263
  • 2
  • 14
  • 35
0
votes
0 answers

How to make my endpoint respond to a SQL Union of two models in FastAPI?

I have two models in my API using FastAPI. Model 1: Deposits (id, created_at, updated_at, value) Model 2: Withdrawals(id, created_at, updated_at, value) And I want to make an endpoint that returns the union of these two models as in SQL (one below…
0
votes
0 answers

Why sqlalchemy bulk update method is slower than raw update queries on large table

I have a table that has around 400k rows and I am trying to bulk update 800 of them into two batches of 400 records each(sql server database). Database table has no triggers or indexes expect the default one on the primary id key. When I use the…
0
votes
0 answers

FastAPI databases wrapper- asyncpg, global database connection pool and ability to set the schema at run time in multi tenant app

How can I implement a safe and efficient global database connection pool in a multi-tenant application using the PostgreSQL Schema-based approach, FastAPI, and asyncpg (databases wrapper)? Specifically, I need to dynamically set the appropriate…
0
votes
0 answers

Define schema for a model field in sqlmodel without adding another table

I am defining a model for companies using SQLModel the code thus far is as follows: from typing import Optional from sqlmodel import SQLModel, Field, JSON from sqlalchemy import Column from pydantic import validator class Company(SQLModel,…
0
votes
1 answer

Dynamically define class with inheritance and static attributes (sqlmodel / sqlalchemy metaclasses)

I have the following class definition that I would like to make dynamic: class SQLModel_custom(SQLModel, registry=self.mapper_registry): metadata = MetaData(schema=self.schema) I've tried something like that: type('SQLModel_custom', (SQLModel,…
ibi0tux
  • 2,481
  • 4
  • 28
  • 49
0
votes
1 answer

Why does grouping by one column return a list of strings instead of a list of dictionaries in SQLModel?

Explanation: The provided code uses the SQLModel library in Python to create a SQLite database and store records in it. The Model class represents the schema of the database table and inherits from the SQLModel class. Two functions,…
0
votes
1 answer

SQLModel many-many relationships not working

I've been struggling with this for weeks now. Here's the relevant code: Base = declarative_base() class Band_Genre(SQLModel, table=True): genre_id: Optional[int] = Field(default=None, foreign_key="FK_Band_Genre_Genre", primary_key=True) …
MichaelD
  • 1,274
  • 1
  • 10
  • 16