Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Typeorm doesn't return generated data id

Im using Typeorm (v8.0.2) and Nestjs(v8) with Nodejs(v16). My problem is when I create a book Typeorm doesn't return generated book id

Here is Book.entity

@Entity()
export class Book {

@PrimaryGeneratedColumn('increment')
id: number;

@Column()
title: string;

@Column()
author: string;
}

And this is book.service

async createBook(createBookDto: CreateBookDto): Promise<Book> {
  const book = await this.bookRepository.create(createBookDto)
  await this.bookRepository.save(createBookDto)
  return book
}

and when I use postman and create a Book it just returns

{
   title: "example"
   author: "foo"
}

id of generated book is missing

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

TL;DR: return result of this.bookRepository.save(createBookDto), not this.bookRepository.create(createBookDto)

From the docs:

create - Creates a new instance of User. Optionally accepts an object literal with user properties which will be written into newly created user object.

const user = repository.create(); // same as const user = new User();
const user = repository.create({
    id: 1,
    firstName: "Timber",
    lastName: "Saw"
}); // same as const user = new User(); user.firstName = "Timber"; user.lastName = "Saw";

In your example you are using @PrimaryGeneratedColumn() decorator that uses database level auto-increment function. The value of this column will be generated after the save() method, not after create().

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!