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

How to implement Foreign Key in sqlmodel in fastapi

models.py from typing import Optional from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select, Column, VARCHAR class User(SQLModel, table=True): __tablename__ = 'users' user_id: Optional[int] =…
code_10
  • 155
  • 1
  • 2
  • 10
0
votes
0 answers

Should I define automatic uuid in alembic migrations?

I have a FastAPI application and in my Item table the field id is an uuid. I'm wondering if I should handle the automatic assigning of id in my models.py or in the Alembic migration file? At the moment this is what I have in my models: class…
lr_optim
  • 299
  • 1
  • 10
  • 30
0
votes
1 answer

How to correctly use JOINs with SQLModel

I have two models - User and UserRole. I want to be able to get the user role together with the related user model. I managed to do it like this: @staticmethod async def get_users(session: AsyncSession): users = await session.execute( …
Or Nakash
  • 1,345
  • 1
  • 9
  • 22
0
votes
1 answer

FastAPI sqlmodel get all rows from the database

I have a FastAPI application that needs to provide GET route to get all Items from a Postgres 13 database. The Item is defined and added from another app. The Item has a model like this: class ItemDb(SQLModel, table=True): __tablename__ =…
lr_optim
  • 299
  • 1
  • 10
  • 30
0
votes
2 answers

Fastapi app: empty array or TypeError: Boolean value of this clause is not defined

So, Im doing simple todo-api app with fastapi and sqlmodel. Migrations went fine, but if I run my server, I dont see anything except empy array. I added some data in db file with DB Browser for SQLite, so it isn't empty. And when I run my server and…
0
votes
0 answers

why subhero table is not creating in sqlmodel?

subhero table is not creating.. it's inheriting from hero class.. from typing import Optional from sqlmodel import Field, BigInteger, Column, SQLModel, JSON from engine import engine class Hero(SQLModel, table=True): name: Optional[str] =…
user18160509
0
votes
1 answer

python SqlModel official docs example not working

I am new to databases and learning sqlmodel for my pysimplegui project. I am following their offical documentation, where i tried this example: example its a simple beginner example, but it seem not working on my machine, i have followed all the…
0
votes
1 answer

How to make a double foreign key as a primary key in SQLModel resp. SQLAlchemy

Currently, I have two tables, user and groups and I want to associate them in table group2user, where I specify who has which rights to a group table. Hence, I need two foreign keys in group2user, which should be able to do cascading delete (if we…
tobias
  • 501
  • 1
  • 6
  • 15
0
votes
0 answers

SQLModel ForeignKeyViolationError in Relationship deletion

WIth SQLModel in Python, I want to create one item of user and one of group associated to it. class User(SQLModel, table=True): id: str = Field(primary_key=True, nullable=False) group: Optional["Group"] = Relationship( …
tobias
  • 501
  • 1
  • 6
  • 15
0
votes
0 answers

Loading a SQL Alchemy Model with relationships without triggering an insert

The problem I am having is When I create my SQLAlchemy model without loading the relationships and immediately commit after, no action is taken as expected (Great). But when I create it with the relationships loaded (based on some ids passed in) and…
user20115832
0
votes
0 answers

Can I mix both sqlalchemy and sqlmodel as ORM

My model base class is like below from sqlalchemy.ext.declarative import as_declarative from sqlalchemy.ext.declarative import declared_attr @as_declarative() class Base: __name__: str # to generate tablename from classname …
Dibshare
  • 95
  • 2
  • 13
0
votes
1 answer

How to create table in existing database with sqlmodel

I am creating a my sqlite Database tables following this link https://sqlmodel.tiangolo.com/tutorial/connect/create-connected-tables/ . Now this creates two tables Team and Hero on start up of my FastAPI application. But on a special use-case I may…
Dibshare
  • 95
  • 2
  • 13
0
votes
1 answer

Fetch data from 3 different tables with SQLAlchemy or SQLModel

I want to fetch data from 3 tables with SQLAlchemy or SQLModel. For example lets say that my tables are the following: class A(SQLModel, table=true): id: int title: str class B(SQLModel, table=true): id: int a_id:…
Cheekou
  • 35
  • 6
0
votes
1 answer

How do I write a validator that checks whether a record has only a single field with int=1 among several?

How do I write a validator that checks whether a record has a single action? I tried the following class MouseAction(SQLModel): user_id: int = Field(foreign_key=user.id) blur: int = Field(default=0) focus: int = Field(default=0) …
HALLOWEEN
  • 3
  • 4
0
votes
1 answer

How to add obj to db with sqlmodel

I am new to FastAPI. How can i create record in db using sqlmodel and databases packages? class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str = Field(index=True) secret_name: str age:…
haku
  • 35
  • 5