Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

241
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda