0
0
Flaskframework~10 mins

Many-to-many relationships 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 the association table for a many-to-many relationship in Flask-SQLAlchemy.

Flask
association_table = db.Table('association',
    db.Column('left_id', db.Integer, db.ForeignKey('left.id')),
    db.Column('right_id', db.Integer, [1]('right.id'))
)
Drag options to blanks, or click blank then click option'
AForeignKey
Brelationship
CColumn
DInteger
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'relationship' instead of 'ForeignKey' in the association table columns.
Forgetting to use 'ForeignKey' causes errors linking tables.
2fill in blank
medium

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

Flask
class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    children = db.relationship('[1]', secondary=association_table, back_populates='parents')
Drag options to blanks, or click blank then click option'
AParents
Bchild
Cchildren
DChild
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names instead of the exact class name.
Confusing the attribute name with the class name.
3fill in blank
hard

Fix the error in the relationship definition to correctly link both sides of a many-to-many relationship.

Flask
class Child(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    parents = db.relationship('Parent', secondary=association_table, [1]='children')
Drag options to blanks, or click blank then click option'
Abackref
Bforeign_keys
Cback_populates
Dlazy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'backref' instead of 'back_populates' when both sides are declared.
Omitting the linking keyword causes relationship errors.
4fill in blank
hard

Fill both blanks to create a many-to-many relationship with a custom association table and back_populates.

Flask
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    books = db.relationship('[1]', secondary=[2], back_populates='authors')
Drag options to blanks, or click blank then click option'
ABook
Bauthor_book
CAuthorBook
Dbook_author
Attempts:
3 left
💡 Hint
Common Mistakes
Using the class name of the association table instead of the table variable.
Mixing up the order of blanks.
5fill in blank
hard

Fill all three blanks to define a many-to-many relationship with a custom association table and back_populates on both models.

Flask
class Book(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    authors = db.relationship('[1]', secondary=[2], back_populates='[3]')
Drag options to blanks, or click blank then click option'
AAuthor
Bauthor_book
Cbooks
Dauthors
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural for the related model name.
Confusing the back_populates attribute name.