In SQLAlchemy, a many-to-many relationship is represented using an association table. This table contains foreign keys that reference the primary keys of the two tables that are involved in the many-to-many relationship. Here’s a step-by-step guide to setting up a many-to-many relationship in SQLAlchemy:
- Define the association table: This table contains only foreign keys and optionally other columns.
- Define the models: Define the two tables involved in the many-to-many relationship.
- Set up the relationship: Use SQLAlchemy’s
relationship
function to establish the many-to-many relationship.
Here is an example with two models: Student
and Course
, which have a many-to-many relationship through an enrollment
association table.
Step 1: Define the Association Table
from sqlalchemy import Table, Column, Integer, ForeignKey from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() enrollment = Table('enrollment', Base.metadata, Column('student_id', Integer, ForeignKey('students.id'), primary_key=True), Column('course_id', Integer, ForeignKey('courses.id'), primary_key=True) )
Step 2: Define the Models
from sqlalchemy import Column, Integer, String from sqlalchemy.orm import relationship class Student(Base): __tablename__ = 'students' id = Column(Integer, primary_key=True) name = Column(String) courses = relationship('Course', secondary=enrollment, back_populates='students') class Course(Base): __tablename__ = 'courses' id = Column(Integer, primary_key=True) title = Column(String) students = relationship('Student', secondary=enrollment, back_populates='courses')
Step 3: Create the Database and Tables
from sqlalchemy import create_engine engine = create_engine('sqlite:///school.db') Base.metadata.create_all(engine)
Step 4: Establishing the Session
from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=engine) session = Session()
Step 5: Adding Data
# Create new students and courses student1 = Student(name='John Doe') student2 = Student(name='Jane Smith') course1 = Course(title='Math 101') course2 = Course(title='History 101') # Establish many-to-many relationships student1.courses.append(course1) student2.courses.append(course1) student2.courses.append(course2) # Add to session and commit session.add(student1) session.add(student2) session.commit()
Step 6: Querying Data
# Querying students enrolled in Math 101 math_students = session.query(Student).join(enrollment).join(Course).filter(Course.title == 'Math 101').all() for student in math_students: print(student.name) # Querying courses taken by Jane Smith jane_courses = session.query(Course).join(enrollment).join(Student).filter(Student.name == 'Jane Smith').all() for course in jane_courses: print(course.title)
In this example, enrollment
is the association table that links students
and courses
. The relationship
function with the secondary
parameter is used to define the many-to-many relationships in both Student
and Course
classes. The back_populates
parameter ensures that the relationship is bidirectional.
This setup allows you to easily query and manage the many-to-many relationship between students and courses.