Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda