Exploring Model and Schema Relationships with FastAPI: One-to-Many SQLModel Implementation beyond CRUD (Part 14)

Posted by

Model And Schema Relationships – FastAPI Beyond CRUD (Part 14)

Model And Schema Relationships – FastAPI Beyond CRUD (Part 14)

In this article, we will discuss the concept of model and schema relationships in FastAPI, specifically focusing on the one-to-many relationship using SQLModel.

What is a One-To-Many Relationship?

A one-to-many relationship is a type of relationship where one record in a table is associated with multiple records in another table. For example, a single author may have written multiple books. In this case, the author is the “one” side of the relationship, while the books are the “many” side.

Creating a One-To-Many Relationship in FastAPI

To create a one-to-many relationship in FastAPI, we can use the SQLModel library. We define a parent model and a child model, where the child model references the parent model using a foreign key. For example:


from sqlmodel import Field, SQLModel, Relationship

class Author(SQLModel, table=True):
    id: int = Field(primary_key=True)
    name: str

class Book(SQLModel, table=True):
    id: int = Field(primary_key=True)
    title: str
    author_id: int = Field(foreign_key="author.id")
    author: Author = Relationship()
	

In the above example, we have defined two models, Author and Book. The Book model has a foreign key field author_id that references the id field of the Author model. We also define a relationship author that specifies the relationship between the two models.

Querying One-To-Many Relationships

Once we have defined our models and relationships, we can query one-to-many relationships using FastAPI. For example, to get all books written by a specific author, we can use the following code:


@app.get("/author/{author_id}/books")
def get_books_by_author(author_id: int):
    author = session.query(Author).filter(Author.id == author_id).first()
    books = session.query(Book).filter(Book.author_id == author.id).all()
    return books
	

In this code snippet, we first query the author by their id, then we query all books that have the same author_id as the author we just obtained. We then return the list of books.

Conclusion

Model and schema relationships are an important concept in FastAPI, especially when dealing with more complex data structures. By using the SQLModel library, we can easily define and query one-to-many relationships in our APIs. This allows us to build more advanced and flexible applications that can handle a wide variety of data relationships.

Stay tuned for future articles where we will explore more advanced topics in FastAPI Beyond CRUD!

0 0 votes
Article Rating
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@SsaliJonathan
2 months ago

Hi guys, Thanks for watching! Kindly click the like button. Also please help me know what you feel about the video

@faruksevinc6200
2 months ago

Hello, I'm new to fastapi. In what order should I watch the videos, you have more than one list. Also, will there be a list on real world applications?

@suen-tech
2 months ago

Superb

@kenshinhimura8679
2 months ago

Will you implement message queues and Celery in this course?

@jaymartinez311
2 months ago

Great series. My question is do we have to use the Optional or does passing a default value do the same thing?

@DennisIvy
2 months ago

🔥