All quizzesMedium
Relations & QueryBuilder — Series 2
Preview — 3 of 10 questions
What does @JoinColumn declare?
javascript
@Entity()
export class Cat {
@ManyToOne(() => Owner, owner => owner.cats)
@JoinColumn({ name: 'owner_id' })
owner: Owner;
}AThat the relation should be loaded eagerly whenever a Cat is read
BThat this side owns the relation and therefore carries the foreign-key column — cat.owner_id — which is what makes Cat the side whose changes actually update the link
CThat a join table should be created to connect the two entities
DThat the relation is read-only from this side
How do these two options differ?
javascript
@OneToMany(() => Comment, comment => comment.post, { cascade: ['insert'] })
comments: Comment[];
@ManyToOne(() => Post, post => post.comments, { onDelete: 'CASCADE' })
post: Post;AThey are synonyms, expressed on the two sides of the relation
Bcascade applies to reads and onDelete to writes
ConDelete is an ORM-level setting and cascade a database constraint
Dcascade is an ORM behaviour — saving a Post also inserts its new Comments in the same operation — while onDelete: 'CASCADE' is emitted into the foreign-key constraint, so the database removes children when a parent row is deleted, including by raw SQL
What is the difference?
javascript
qb.leftJoinAndSelect('cat.owner', 'owner') // (1)
qb.leftJoin('cat.owner', 'owner').where('owner.city = :city', { city }) // (2)ABoth join the table, but only (1) adds the joined entity's columns to the SELECT list and hydrates cat.owner; (2) joins purely so the relation can be filtered on, leaving cat.owner undefined
B(2) produces an inner join despite its name
C(1) issues two queries and (2) one
DThey are equivalent; AndSelect is a readability suffix
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.