Skip to Content
Introduction to Flask-SQLAlchemy
Part II: declaring relationships (Foreign keys)

In the previous lesson, we began adding a one-to-many relationship to the Book and Reader models by using .relationship(). But that does not completely specify our one-to-many relationship. We additionally have to specify what the foreign keys are for the model on the ‘many’ side of the relationship. To remind you, a foreign key is a field (or collection of fields) in one table that refers to the primary key in another table.

In this exercise we want to create the following database schema: title

To complete the schema, we need to add the Review model, and specify the foreign keys (blue arrows) representing the following relationship:

  • One review ——– one book for which the review was written
  • One review ——– one reader who wrote that review

The red arrows were covered in the previous exercise with the db.relationship() columns.

Similar to the previous models we declared, the Review model has its own columns such as text, stars (denoting ratings), and its own primary key field id. Review additionally needs to specify which other models it is related to by specifying their primary key in its foreign key column:

book_id = db.Column(db.Integer, db.ForeignKey(''))

The book_id field is a foreign key that refers to the primary key id of the Book table. Similar to the primary key, a foreign key is just another column in our model with unique entries.



Add a new field to Review called reviewer_id that serves as a foreign key to the Reader class.

Folder Icon

Sign up to start coding

Already have an account?