Back to Writing
NOTESflaskpythonsqlalchemyforeign-keyorm
Flask - Question and Answer Models with Foreign Keys
December 22, 2020•Updated Feb 17, 2026
The Question model is created by passing data directly to the constructor. The Answer model works similarly, but with one difference: the Answer model's question property directly retrieves and stores the question data being answered.
Since the question property is a foreign key, you can insert question data directly. Using the data browser, you can verify that the ForeignKey (question_id) automatically acquires the ID of the question being answered.