Repository navigation
How can I query or order_by with Link Model with Extra Fields in a many-to-many relation? #819
First Check
Commit to Help
Example Codeclass UserRoomLink(SQLModel, table=True):
room_id: Optional[int] = Field(
default=None, foreign_key="room.id", primary_key=True
)
user_id: Optional[int] = Field(
default=None, foreign_key="user.id", primary_key=True
)
created_at: datetime = Field(
default_factory=datetime.utcnow, index=True, nullable=False
)
class Room(RoomBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
users: List["User"] = Relationship(back_populates="rooms", link_model=UserRoomLink)
class User(UserBase, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
rooms: Optional[Room] = Relationship(
back_populates="users", link_model=UserRoomLink
)DescriptionHow can I query or order_by with Link Model with Extra Fields? Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.14 Python Version3.10.5 Additional ContextNo response |
Answered by
YuriiMotov
Aug 21, 2025
Replies: 2 comments 1 reply
|
I am experimenting with something akin to this: But seems to be perfectly ignored on an object... (unless I need to re-query object or something like that). |
0 replies
|
You can specify Try changing Runable code example in the details: Details |
1 reply
Answer selected by
YuriiMotov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can specify
sa_relationship_kwargs={"order_by": "UserRoomLink.created_at.desc()"}to your relationship attribute:Try changing
"order_by": "UserRoomLink.created_at"to"order_by": "UserRoomLink.created_at.desc()"and see that order will be different.Runable code example in the details:
Details