sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

Problem with relationship in SQLmodel and fastapi

Open Beafowl-Pull opened this issue 2 years ago • 1 comments

First Check

  • [X] I added a very descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the SQLModel documentation, with the integrated search.
  • [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
  • [X] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [X] I already checked if it is not related to SQLModel but to Pydantic.
  • [X] I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • [X] I commit to help with one of those options 👆

Example Code

from typing import Optional, List, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship

from app.models.formation_user import FormationUser

if TYPE_CHECKING:
    from app.models.formation import Formation
    from app.models.formation import FormationRead

class UserBase(SQLModel):
    is_admin: bool = Field(default=None)
    name: Optional[str] = None
    email: Optional[str] = None
    password: Optional[str] = None


class User(UserBase, table=True):
    id: int = Field(default=None, primary_key=True)
    formations: List["Formation"] = Relationship(back_populates="users", link_model=FormationUser)


and 

from typing import Optional, List, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship
from app.models.formation_user import FormationUser

if TYPE_CHECKING:
    from app.models.user import User


class FormationBase(SQLModel):
    formation_name: Optional[str] = None
    program_description: Optional[str] = None
    objective_description: Optional[str] = None
    who: Optional[str] = None
    requirements: Optional[str] = None
    duration_days: Optional[int] = None
    duration_hours: Optional[int] = None
    access_delay: Optional[int] = None
    price: Optional[int] = None
    disable_access: Optional[bool] = None
    satisfaction_rate: Optional[float] = Field(default=None)
    owner_id: Optional[int] = Field(default=None, foreign_key="user.id")

connected using this db :

from typing import Optional, List, TYPE_CHECKING
from sqlmodel import SQLModel, Field, Relationship


class FormationUser(SQLModel, table=True):
    user_id: Optional[int] = Field(default=None, foreign_key="user.id", primary_key=True)
    formation_id: Optional[int] = Field(default=None, foreign_key="formation.id", primary_key=True)

Description

I tried to create a relation between 2 database with an intermediate database but it's not displayed in my /docs (i m using fastapi) I using multiple files for models and juste basic request sql (see below). I've read another github issue that say that i need to downgrade my version of sqlalchemy and upgrade my version of fastapi but nothing make it works, i need to know if it's a problem with sqlmodel or if i am the problem.

Operating System

Linux

Operating System Details

I use arch with a local database on postgres

SQLModel Version

latest (0.0.6)

Python Version

3.10

Additional Context

the function that i use for post a user by my docs on fastapi:

async def post_users(user: UserCreate): with Session(engine) as session: db_user = User.from_orm(user) session.add(db_user) session.commit() session.refresh(db_user) return db_user

Beafowl-Pull avatar Jul 21 '22 07:07 Beafowl-Pull

https://github.com/tiangolo/sqlmodel/issues/383#issuecomment-1193373160

markzhang0928 avatar Jul 29 '22 09:07 markzhang0928