DB Relationships & Migrations
DB Relationships & Migrations
Right now we have two models, Product and User. To build out a store front and process orders, we'll need at least one more model: Store
. We can also add an Order
model if we want to track orders.
Each User
needs to be able to manage one Store
. Each Store
will have multiple Product
s. Every Order
will have one Product
.
This data model is relational, but neither our database nor our models currently reflect that. For example, there is no way to denote that a Product
was created by a specific User
.
To model relationships at a database level, the bare minimum we need is to have a field in the database to store the ID of the related record. If our field on Product
was called creator_id
, we could manually query for all Product
s where creator_id
is equal to a specific User
ID. This would let us manually query for all Products created by a specific user ID.
This page is a preview of Fullstack Flask: Build a Complete SaaS App with Flask