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
17
votes
2 answers

How to use JSON columns with SQLModel

I'm trying to define a JSON column via SQLModel: from typing import Optional from sqlmodel import Field, Session, SQLModel, create_engine, JSON class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) …
danwos
  • 416
  • 3
  • 12
13
votes
2 answers

How to get Alembic to recognise SQLModel database model?

Using SQLModel how to get alembic to recognise the below model? from sqlmodel import Field, SQLModel class Hero(SQLModel, table=True): id: int = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] =…
Greg
  • 8,175
  • 16
  • 72
  • 125
9
votes
3 answers

SQLModel: sqlalchemy.exc.ArgumentError: Column expression or FROM clause expected,

I am using the SQLModel library to do a simple select() like described on their official website. However I am getting Column expression or FROM clause expected error message from typing import Optional from sqlmodel import Field, Session,…
joooet
  • 105
  • 1
  • 1
  • 5
7
votes
2 answers

fastapi fastapi-users with Database adapter for SQLModel users table is not created

I was trying to use fastapi users package to quickly Add a registration and authentication system to my FastAPI project which uses the PostgreSQL database. I am using asyncio to be able to create asynchronous functions. In the beginning, I used only…
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
6
votes
5 answers

FastAPI - "TypeError: issubclass() arg 1 must be a class" with modular imports

When working with modular imports with FastAPI and SQLModel, I am getting the following error if I open /docs: TypeError: issubclass() arg 1 must be a class Python 3.10.6 pydantic 1.10.2 fastapi 0.85.2 sqlmodel 0.0.8 macOS 12.6 Here is a…
Felix
  • 167
  • 1
  • 8
6
votes
1 answer

How to generate a UUID field with FastAPI, SQLalchemy, and SQLModel

I'm struggling to get the syntax to create a UUID field when creating a model in my FastAPI application. I'm using SQLModel. So basically, my models.py file looks like this: from datetime import datetime from typing import Optional import uuid from…
John Kealy
  • 1,503
  • 1
  • 13
  • 32
6
votes
1 answer

How we can migrate database in sqlmodel?

class Hero(SQLModel, table=True): id: int = Field(primary_key=True) name: str age: int = Field(default=None) status:str I created the table using SQLModel.metadata.create_all(engine), when I changed the type of status to str and run…
faris404
  • 343
  • 1
  • 3
  • 11
5
votes
1 answer

Dynamically set sql-default value with table-name in SQLModel

I'm trying to create a base-class in SQLModel which looks like this: class BaseModel(SQLModel): @declared_attr def __tablename__(cls) -> str: return cls.__name__ guid: Optional[UUID] = Field(default=None,…
Matthias Burger
  • 5,549
  • 7
  • 49
  • 94
5
votes
1 answer

MissingGreenlet: greenlet_spawn has not been called

I am trying to get the number of rows matched in a one to many relationship. When I try parent.children_count I get : sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an…
Jake
  • 245
  • 1
  • 3
  • 13
5
votes
1 answer

Composite Indexes SQLModel

I'm experimenting with SQLModel (https://sqlmodel.tiangolo.com/) and I get to the point that I had to create a composite index between several fields and I can't how to do it using SQLModel library. Db Model The only work around I found was to use…
5
votes
1 answer

one-to-one relationships with sqlmodel

After working through the tutorial of SQLModel, I don't remember seeing anything on how to implement 1:1 relationships using Relationship attributes. I found documentation for SQLAlchemy, but it's not immediately clear how this applies to…
clstaudt
  • 21,436
  • 45
  • 156
  • 239
5
votes
0 answers

how to use Enum types with SqlModel, and alembic

I'm trying to find a way to get SqlModel and Alembic play nice together. My goal is to not have to hand-edit the auto-generated alembic migrations. Here's my model class: class SongBase(SQLModel): name: str artist: str label: str =…
Trondh
  • 3,221
  • 1
  • 25
  • 34
4
votes
1 answer

How to define a UniqueConstraint on two or more columns with SQLModel?

With SQLAlchemy it is possible to create a constraint on two or more columns using the UniqueConstraint declaration. What would be the most idiomatic way of doing that using SQLModel? For example: from sqlmodel import SQLModel, Field class…
Jundiaius
  • 6,214
  • 3
  • 30
  • 43
4
votes
1 answer

Using SQLAlchemy declarative base with SQL Model

In one of our project, we used SQL Alchemy declarative Base for all of our models for years (and there is many). We want to try the new SQLModel library for our latest model declaration. For that, we tried to declare it separately from the Base…
jossefaz
  • 3,312
  • 4
  • 17
  • 40
4
votes
1 answer

With fastapi/SQLModel, how to specify the order the columns/fields are shown in a table/response object?

I have the following models/schemas: class UserBase(SQLModel): full_name: str email: EmailStr is_active: bool = True is_superuser: bool = False class UserRead(UserBase): id: uuid.UUID class UserCreate(UserBase,…
gmagno
  • 1,770
  • 1
  • 19
  • 40
1
2 3
11 12