Bird
0
0

Which is the correct entity definition?

hard📝 Application Q15 of 15
NestJS - Database with TypeORM
You want to define an entity Order with an auto-generated primary key id, a string customerName limited to 150 characters, and an optional numeric discount that can be null. Which is the correct entity definition?
Aimport { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: number; @Column({ length: 150 }) customerName: string; @Column({ type: 'int', nullable: false }) discount: number; }
Bimport { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: string; @Column({ length: 150 }) customerName: string; @Column({ type: 'float' }) discount: number; }
Cimport { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: number; @Column({ type: 'varchar', length: 150 }) customerName: number; @Column({ type: 'float', nullable: true }) discount: number; }
Dimport { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'; @Entity() export class Order { @PrimaryGeneratedColumn() id: number; @Column({ length: 150 }) customerName: string; @Column({ type: 'float', nullable: true }) discount?: number; }
Step-by-Step Solution
Solution:
  1. Step 1: Verify primary key definition

    @PrimaryGeneratedColumn() on id: number creates an auto-incrementing integer primary key. A string type would be incorrect without additional configuration like UUID.
  2. Step 2: Check customerName

    @Column({ length: 150 }) with string type creates a VARCHAR(150) column. A number type mismatches.
  3. Step 3: Confirm discount

    @Column({ type: 'float', nullable: true }) with optional discount?: number allows null values. Missing nullable: true, wrong type like 'int', or non-optional fails.
  4. Final Answer:

    The entity with number id, string customerName({length:150}), optional nullable float discount -> Option D
  5. Quick Check:

    Match types, nullable, and optional properties carefully [OK]
Quick Trick: Use nullable: true and optional property for nullable columns [OK]
Common Mistakes:
  • Mismatching property types and column types
  • Forgetting to mark nullable columns as optional
  • Using wrong primary key type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes