Back to Writing
NOTESflaskpythonsqlalchemyforeign-keyorm

Flask - Creating Models with Foreign Key Relationships

December 14, 2020Updated Feb 17, 2026

Post image

This post summarizes content from "Do it! Jump to Flask."

Sincere thanks to author Park Eung-yong and publisher "EasySpub" for making this content possible.

Do it! Jump to Flask

Post image

Just like with the Question model, data was created by passing it directly through the constructor.

However, there is one difference.

I directly retrieved and inserted the question data into the Answer model's question property.

Post image

Since the question property is a foreign key, you can insert question data directly.

Checking with the data browser:

Post image

You can see that the ForeignKey question_id automatically retrieves the ID of the question we're answering.

Post image

And since we also set up a reverse reference:

Post image

You can look up the question directly from the answer like this.

That wraps up model creation and usage.

Starting from the next post, I'll cover implementing the question list query feature.


Do it! Jump to Flask