Skip to content
Discussion options

You must be logged in to vote

You can specify the sort order for relationship field:

heroes: List["Hero"] = Relationship(back_populates="team", sa_relationship_kwargs={"order_by": "Hero.name"})

Runnable code example in the details:

from typing import Optional, List
from sqlmodel import SQLModel, Field, Relationship, create_engine, Session, select


class Team(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    headquarters: str

    heroes: List["Hero"] = Relationship(back_populates="team", sa_relationship_kwargs={"order_by": "Hero.name"})


class Hero(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str
    secret_name: s…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
2 participants