blog_app/
├── main.py
├── models.py
├── database.py-
Model Association:
- Relationships establish how one model (
table) is related to another. For example:- A
Usercan have multiplePostobjects (One-to-Manyrelationship). - A
Postcan have multipleCommentobjects (One-to-Manyrelationship). - A
Commentis associated with oneUserand onePost(Many-to-Onerelationship).
- A
- Relationships establish how one model (
-
Setting up Relationships:
- Relationships are declared using the
Relationshipfunction fromsqlmodel. - They use the
back_populatesparameter to establish two-way connections between models.
- Relationships are declared using the
-
Foreign Keys:
- Foreign keys explicitly store the reference in the database to the related record.
- For example, in the
Postmodel, theauthor_idfield is a foreign key (Field(foreign_key="user.id")) linking a specific post to a user
