Given two models Author and Book connected by a many-to-many relationship through author_book table, what will print([book.title for book in author.books]) output after adding two books to an author?
from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() author_book = db.Table('author_book', db.Column('author_id', db.Integer, db.ForeignKey('author.id')), db.Column('book_id', db.Integer, db.ForeignKey('book.id')) ) class Author(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) books = db.relationship('Book', secondary=author_book, back_populates='authors') class Book(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) authors = db.relationship('Author', secondary=author_book, back_populates='books') # Create author and books author = Author(name='Alice') book1 = Book(title='Flask Basics') book2 = Book(title='Advanced Flask') author.books.append(book1) author.books.append(book2) print([book.title for book in author.books])
Think about what author.books contains after appending two book objects.
The author.books list contains the two Book objects added. Printing their titles shows both book names.
Choose the correct code snippet that defines a many-to-many relationship between Student and Course models using an association table.
Remember the association table needs foreign keys and both sides should use secondary with back_populates.
Option B correctly defines the association table with foreign keys and sets up db.relationship on both models with secondary and back_populates for bidirectional access.
Consider this code snippet. Why does it raise an error when trying to add a book to an author?
author_book = db.Table('author_book', db.Column('author_id', db.Integer, db.ForeignKey('author.id')), db.Column('book_id', db.Integer, db.ForeignKey('book.id')) ) class Author(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) books = db.relationship('Book', secondary=author_book) class Book(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) # Usage author = Author(name='Bob') book = Book(title='Flask Tips') author.books.append(book) db.session.add(author) db.session.commit()
Think about what SQLAlchemy needs to track objects before committing.
SQLAlchemy requires all objects to be added to the session before commit. Here, book is not added, so it raises an error.
len(course.students) after these operations?Given Student and Course models with a many-to-many relationship, what is the length of course.students after adding two students but removing one?
enrollments = db.Table('enrollments', 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=enrollments, back_populates='students') class Course(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) students = db.relationship('Student', secondary=enrollments, back_populates='courses') student1 = Student(name='John') student2 = Student(name='Jane') course = Course(title='Math 101') course.students.append(student1) course.students.append(student2) course.students.remove(student1) print(len(course.students))
Think about what happens when you remove one student from the list.
After adding two students, removing one leaves exactly one student in the course's students list.
In Flask SQLAlchemy many-to-many relationships, what is the main purpose of the association table?
Think about what the association table contains and why it is needed.
The association table holds foreign keys from both related tables to link them, enabling many-to-many relationships without needing a full model class.