0
0
Expressframework~10 mins

Associations (hasMany, belongsTo) in Express - 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 association where a User has many Posts.

Express
User.[1](Post);
Drag options to blanks, or click blank then click option'
AbelongsToMany
BbelongsTo
ChasOne
DhasMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using belongsTo instead of hasMany reverses the relationship.
Using hasOne limits to only one related record.
2fill in blank
medium

Complete the code to define the inverse association where a Post belongs to a User.

Express
Post.[1](User);
Drag options to blanks, or click blank then click option'
AbelongsTo
BhasOne
ChasMany
DbelongsToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using hasMany here would incorrectly imply a Post owns many Users.
Using hasOne is not correct for the inverse of hasMany.
3fill in blank
hard

Fix the error in the association code to correctly link Comment to Post.

Express
Comment.[1](Post);
Drag options to blanks, or click blank then click option'
AhasMany
BbelongsTo
ChasOne
DbelongsToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using hasMany here would imply Comment owns many Posts, which is incorrect.
Using hasOne would limit Comment to only one Post but is less clear than belongsTo.
4fill in blank
hard

Fill both blanks to create a bidirectional association between Author and Book.

Express
Author.[1](Book);
Book.[2](Author);
Drag options to blanks, or click blank then click option'
AhasMany
BbelongsTo
ChasOne
DbelongsToMany
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping hasMany and belongsTo reverses the relationship.
Using belongsToMany is for many-to-many, not one-to-many.
5fill in blank
hard

Fill all four blanks to define a many-to-many association between Student and Course using Enrollment.

Express
Student.[1](Course, { through: Enrollment });
Course.[2](Student, { through: Enrollment });
Enrollment.[3](Student);
Enrollment.[4](Course);
Drag options to blanks, or click blank then click option'
AhasMany
BbelongsTo
CbelongsToMany
DhasOne
Attempts:
3 left
💡 Hint
Common Mistakes
Using hasMany or hasOne instead of belongsToMany for many-to-many.
Not linking the join table back to the models with belongsTo.