0
0
Flaskframework~10 mins

Relationships (one-to-many) in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a one-to-many relationship in Flask-SQLAlchemy.

Flask
class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    children = db.relationship('[1]', backref='parent')
Drag options to blanks, or click blank then click option'
AParent
Bchild
Cchildren
DChild
Attempts:
3 left
💡 Hint
Common Mistakes
Using the lowercase or plural form instead of the class name.
Using the current class name instead of the related class.
2fill in blank
medium

Complete the code to define the foreign key in the child model.

Flask
class Child(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, db.ForeignKey('[1]'))
Drag options to blanks, or click blank then click option'
Aparent.id
Bparent_id
CParent.id
Dchild.id
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name with a dot instead of the table name.
Using the column name alone without the table name.
3fill in blank
hard

Fix the error in the relationship definition to enable lazy loading.

Flask
class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    children = db.relationship('Child', [1]='dynamic')
Drag options to blanks, or click blank then click option'
Alazy
Bbackref
Cforeign_key
Duselist
Attempts:
3 left
💡 Hint
Common Mistakes
Using backref or foreign_key instead of lazy.
Misspelling the parameter name.
4fill in blank
hard

Fill both blanks to define a back reference and enable cascade delete.

Flask
class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    children = db.relationship('Child', backref=[1], cascade=[2])
Drag options to blanks, or click blank then click option'
A'parent'
B'all, delete-orphan'
C'children'
D'save-update'
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural or incorrect backref names.
Omitting quotes around string values.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension mapping child names to their ages for children older than 5.

Flask
child_ages = {child.[1]: child.[2] for child in parent.children if child.[3] > 5}
Drag options to blanks, or click blank then click option'
Aname
Bage
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong attribute names.
Mixing up keys and values in the dictionary.