sqlmodel

by_alias parameter on model_dump() is being ignored

by_alias parameter on model_dump() is being ignored Question: In the following code, we see that the field id is indeed created with the alias of user_id when we print the model_fields. However, when I then call model_dump(alias=True), the returned dict has an id key, but does not have a user_id key as I am expecting. …

Total answers: 2

Generate SQLAlchemy models from Pydantic models

Generate SQLAlchemy models from Pydantic models Question: So I have this large JSON dataset with a huge number of properties (as well as nested objects). I have used datamodel-code-generator to build pydantic models. The JSON data has been imported into pydantic models (with nested models). Now I need to push them into the database. How …

Total answers: 1

Alembic – how to create hypertable

Alembic – how to create hypertable Question: How to generate hypertable using Alembic? Some custom call must be added I suppose, but where? I tried event.listen but Alembic does not register it. Asked By: romanzdk || Source Answers: You can create hypertables in Alembic by adding manual, custom, migration actions. You cannot generate it automatically, …

Total answers: 2

How should we manage datetime fields in SQLModel in python?

How should we manage datetime fields in SQLModel in python? Question: 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, TIMESTAMP, text class HeroBase(SQLModel): # essential fields …

Total answers: 1

Could not assemble any primary key columns for mapped table SQLAlchemy

Could not assemble any primary key columns for mapped table SQLAlchemy Question: When im tyring to run my FastAPI application this error appears. sqlalchemy.exc.ArgumentError: Mapper mapped class DTabelle->dtabelle could not assemble any primary key columns for mapped table ‘dtabelle’ When i remove the file d_tabelle.py everything works. I think it can be a duplicate of …

Total answers: 2

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

FastAPI – "TypeError: issubclass() arg 1 must be a class" with modular imports Question: 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 reproducible …

Total answers: 3

greenlet_spawn has not been called

MissingGreenlet: greenlet_spawn has not been called Question: 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 unexpected place? (Background on this error at: https://sqlalche.me/e/14/xd2s) I added …

Total answers: 1

How to dynamically define an SQLModel class

How to dynamically define an SQLModel class Question: 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 the create_model function built into SQLModel and there …

Total answers: 1

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

How to make a double foreign key as a primary key in SQLModel resp. SQLAlchemy Question: 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 …

Total answers: 1