Relationships between models
Relationships between models in SQLAlchemy are links between two or more models that allow models to reference each other automatically. This allows naturally related data, such as comments to posts, to be easily retrieved from the database with its related data. This is where the R in RDBMS comes from, and it gives this type of database a large amount of power.
Let's create our first relation. Our blogging website is going to need some blog posts. Each blog post is going to be written by one user, so it makes sense to link posts back to the user that wrote them to easily get all posts by a user. This is an example of a one-to-many relationship.
One-to-many
Let's add a model to represent blog posts on our website:
class Post(db.Model): id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(255)) text = db.Column(db.Text()) publish_date = db.Column(db.DateTime()) user_id = db.Column(db.Integer(), db.ForeignKey(&apos...