0
0
Flaskframework~20 mins

Relationships (one-to-many) in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
One-to-Many Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask-SQLAlchemy one-to-many relationship query?
Given the models Author and Book with a one-to-many relationship, what will be printed by the following code?
Flask
author = Author.query.filter_by(name='Alice').first()
print(len(author.books))
AThe number of books written by Alice
BThe total number of authors in the database
CThe total number of books in the database
DRaises AttributeError because 'books' is not defined
Attempts:
2 left
💡 Hint
Remember that 'books' is the related collection for the one-to-many relationship from Author to Book.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a one-to-many relationship in Flask-SQLAlchemy?
Choose the correct way to define a one-to-many relationship between Author and Book models.
A
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.relationship('Book', backref='author')

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
B
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey('book.id'))

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.relationship('Author', backref='book')
C
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.relationship('Book', foreign_keys='author.id')

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
D
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.relationship('Book')

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer)
Attempts:
2 left
💡 Hint
The foreign key should be on the 'many' side, and the relationship on the 'one' side.
🔧 Debug
advanced
2:00remaining
Why does accessing author.books raise an error in this code?
Given these models, why does the code 'author = Author.query.first(); print(author.books)' raise an AttributeError?
Flask
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)

class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey('author.id'))
ABecause the backref name is incorrect in the Book model
BBecause the foreign key is missing on the Book model
CBecause the Author model lacks a relationship() definition for 'books'
DBecause the primary key is missing on the Author model
Attempts:
2 left
💡 Hint
Check if the Author model knows about its related books.
state_output
advanced
2:00remaining
What is the value of book.author.name after this code runs?
Given these models and data, what will 'print(book.author.name)' output?
Flask
author = Author(name='Bob')
db.session.add(author)
db.session.commit()

book = Book(title='Flask Guide', author_id=author.id)
db.session.add(book)
db.session.commit()

book = Book.query.first()
print(book.author.name)
AFlask Guide
BRaises AttributeError
CNone
DBob
Attempts:
2 left
💡 Hint
The 'author' attribute on Book is set by the backref from the relationship.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the behavior of lazy='dynamic' in a one-to-many relationship?
In Flask-SQLAlchemy, what does setting lazy='dynamic' on a one-to-many relationship do?
AIt loads all related items immediately when the parent is queried
BIt returns a query object instead of a list, allowing further filtering before loading related items
CIt disables loading of related items entirely
DIt caches the related items permanently after the first access
Attempts:
2 left
💡 Hint
Think about how you can chain filters on the related collection.