0
0
Expressframework~20 mins

Associations (hasMany, belongsTo) in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Association Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding hasMany association behavior

Given the following Sequelize models, what will be the output of console.log(user.posts.length) after fetching the user with posts?

Express
const User = sequelize.define('User', { name: DataTypes.STRING });
const Post = sequelize.define('Post', { title: DataTypes.STRING });
User.hasMany(Post);
Post.belongsTo(User);

async function test() {
  const user = await User.findOne({ where: { id: 1 }, include: [Post] });
  console.log(user.posts.length);
}
AAlways 0, because posts are not loaded automatically
BThe number of posts related to the user (e.g., 3 if user has 3 posts)
CUndefined, because posts is not a property on user
DThrows a runtime error because include is not valid here
Attempts:
2 left
💡 Hint

Remember that hasMany creates an array property on the source model instance when included.

state_output
intermediate
2:00remaining
belongsTo association foreign key value

Consider these Sequelize models and code snippet. What will be the value of post.userId after creating a post linked to a user?

Express
const User = sequelize.define('User', { name: DataTypes.STRING });
const Post = sequelize.define('Post', { title: DataTypes.STRING });
Post.belongsTo(User);
User.hasMany(Post);

async function test() {
  const user = await User.create({ name: 'Alice' });
  const post = await Post.create({ title: 'Hello', userId: user.id });
  console.log(post.userId);
}
AThe id of the user (e.g., 1)
BUndefined, because userId is not set automatically
CNull, because belongsTo does not set foreign keys
DThrows an error because userId is not a defined field
Attempts:
2 left
💡 Hint

Check how belongsTo creates a foreign key field on the source model.

📝 Syntax
advanced
2:00remaining
Correct association declaration syntax

Which option correctly declares a hasMany and belongsTo association between Author and Book models in Sequelize?

Express
const Author = sequelize.define('Author', { name: DataTypes.STRING });
const Book = sequelize.define('Book', { title: DataTypes.STRING });
AAuthor.hasMany(Book); Book.belongsTo(Author);
BAuthor.belongsTo(Book); Book.hasMany(Author);
CAuthor.hasMany(Book, { foreignKey: 'authorId' }); Book.belongsTo(Author, { foreignKey: 'authorId' });
DAuthor.hasMany(Book, { foreignKey: 'bookId' }); Book.belongsTo(Author, { foreignKey: 'authorId' });
Attempts:
2 left
💡 Hint

Check that foreign keys match on both sides of the association.

🔧 Debug
advanced
2:00remaining
Debugging missing association data in query

Given these models and query, why does user.posts return undefined?

Express
const User = sequelize.define('User', { name: DataTypes.STRING });
const Post = sequelize.define('Post', { title: DataTypes.STRING });
User.hasMany(Post);
Post.belongsTo(User);

async function test() {
  const user = await User.findOne({ where: { id: 1 } });
  console.log(user.posts);
}
ABecause the query did not include the Post model, so posts are not loaded
BBecause hasMany association was not declared correctly
CBecause user.posts is always undefined in Sequelize
DBecause the user with id 1 does not exist
Attempts:
2 left
💡 Hint

Think about how Sequelize loads associated data.

🧠 Conceptual
expert
2:00remaining
Understanding cascading deletes with associations

In Sequelize, if you want deleting a User to automatically delete all their Posts, which association option should you use?

APost.belongsTo(User, { onUpdate: 'RESTRICT' });
BUser.hasMany(Post, { onUpdate: 'CASCADE' });
CPost.belongsTo(User, { onDelete: 'SET NULL' });
DUser.hasMany(Post, { onDelete: 'CASCADE' });
Attempts:
2 left
💡 Hint

Think about which side controls deletion behavior and what 'CASCADE' means.