Relationships in Flask-SQLAlchemy help define associations between tables, making it easier to model complex data structures. Relationships are established using db.relationship() and db.ForeignKey().
A single row in one table is associated with multiple rows in another table.
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
books = db.relationship('Book', backref='author', lazy=True)
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
author_id = db.Column(db.Integer, db.ForeignKey('author.id'))- The
booksattribute inAuthordefines the relationship. - The
author_idinBookis a foreign key linking to theAuthortable. - The
backrefparameter allows reverse access (book.author).
A single row in one table is associated with a single row in another table.
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(50), unique=True, nullable=False)
profile = db.relationship('Profile', backref='user', uselist=False)
class Profile(db.Model):
id = db.Column(db.Integer, primary_key=True)
bio = db.Column(db.String(200))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))uselist=Falseensures a one-to-one relationship.
Multiple rows in one table are associated with multiple rows in another table.
association_table = db.Table('association',
db.Column('student_id', db.Integer, db.ForeignKey('student.id')),
db.Column('course_id', db.Integer, db.ForeignKey('course.id'))
)
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
courses = db.relationship('Course', secondary=association_table, backref='students')
class Course(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100))association_tableis a helper table without its own model.secondary=association_tablespecifies the table managing the many-to-many relationship.
Creates a reverse relationship.
books = db.relationship('Book', backref='author')Now you can access book.author or author.books.
Defines how related objects are loaded.
True: Load all data immediately.select: Use a separate select statement (default).joined: Use a join query.subquery: Load with a subquery.
Controls whether the relationship returns a list (True) or a single item (False).
Specifies the table used for a many-to-many relationship.
class Department(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
employees = db.relationship('Employee', backref='department', lazy='dynamic')
class Employee(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
department_id = db.Column(db.Integer, db.ForeignKey('department.id'))- Use
backrefto simplify reverse queries. - Always set foreign keys to maintain referential integrity.
- Use meaningful relationship names for easier access.
By mastering relationships, you can effectively model complex database schemas with Flask-SQLAlchemy.