Skip to content

Are Many-to-Many link supported with fastapi? #1511

Answered by YuriiMotov
Trophime asked this question in Questions
Discussion options

You must be logged in to vote

The initial code doesn't through the TypeError: issubclass() arg 1 must be a class anymore in current version.

To make your code work as intended you probably need to make all fields in MagnetUpdate optional:

class MagnetUpdate(SQLModel):
    name: Optional[str] = None
    mparts: Optional[List["MPart"]] = None

And change the code that updates fields of db_magnet to use sqlmodel_update, then manually update mparts field:

    magnet_data = magnet.model_dump(exclude_unset=True)
    db_magnet = db_magnet.sqlmodel_update(magnet_data)
    if magnet.mparts:
        db_magnet.mparts = [
            MPart.model_validate(mpart_data) for mpart_data in magnet.mparts
        ]

Full runnable code exam…

Replies: 22 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
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
Converted from issue

This discussion was converted from issue #121 on August 13, 2025 09:24.