Python Flask-SQLAlchemy Declaring a Foreign Key field in a Model

For the following exercise in Introduction to Flask-SQLAlchemy (Part II) , why would the foreign key field, reviewier_id be set as:

# ...
reviewer_id = db.Column(..., db.ForeignKey('reader.id'))
# ...

instead of

# ...
reviewer_id = db.Column(..., db.ForeignKey('reviewer.id'))
# ...

given that the Reader model has its backref attribute for its relationship field set as reviewer when referencing the Review model by default in app.py:

from flask import Flask
# ...
class Reader(db.Model):    # LINE 25
    id = db.Column(db.Integer, primary_key = True)
    # ...
    reviews = db.relationship('Review', backref='reviewer', lazy = 'dynamic')    # LINE 30
    # ...

class Review(db.Model):    # LINE 37
    # ...
# ...

EDIT: Added line numbers for easier referencing.

Hello!

In db.ForeignKey() we specify the table name and the column name. Because our table is called Reader and its primary key is id, we get db.ForeignKey('reader.id'). In our project, there is no ‘Reviewer’ entity for ‘db.ForeignKey(‘reviewer.id’)’.

1 Like

edit: Updated reply because follow-up question was irrelevant. cnc (shh) :slight_smile: