如何创建与 NestJS/TypeORM/PostgreSQL 具有多对一关系的新数据库记录?

问题描述 投票:0回答:1

我正在使用 TypeORM (PostgreSQL) 学习 NestJS,并尝试创建一个图书馆网站进行练习。

这是我的实体,

Book
Author
。一位作者可以拥有多本书,但每本书只能属于一位作者。

import { Author } from 'src/author/entities/author.entity';
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Book {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @ManyToOne(() => Author, (author) => author.books)
  author: Author;

  @Column()
  summary: string;

  @Column()
  isbn: string;
}
import { Book } from 'src/book/entities/book.entity';
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Author {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @OneToMany(() => Book, (book) => book.author)
  books: Book[];
}

以下是 DTO:

export class CreateBookDto {
  title: string;
  author: number;
  summary: string;
  isbn: string;
}
export class CreateAuthorDto {
  firstName: string;
  lastName: string;
}

这是向数据库添加新书的方法:

import { Injectable } from '@nestjs/common';
import { CreateBookDto } from './dto/create-book.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Book } from './entities/book.entity';
import { Repository } from 'typeorm';

@Injectable()
export class BookService {
  constructor(
    @InjectRepository(Book) private readonly bookRepository: Repository<Book>,
  ) {}

  create(createBookDto: CreateBookDto) {
    return this.bookRepository.save(createBookDto);
  }
}

典型的请求如下所示:

{
  title: "Example Book Title",
  author: 1,
  summary: "Example Book Summary",
  isbn: "978-3-16-148410-0"
}

作者应该已经在数据库中,并且可以使用预先填充的

<select>
标签来选择,例如。数字是该作者在数据库表中的 id。

我认为我的 DTO 或我创建新书的方式有问题,因为打字稿给了我这个错误:

this.bookRepository.save(createBookDto);

No overload matches this call.
  Overload 1 of 4, '(entities: DeepPartial<Book>[], options?: SaveOptions | undefined): Promise<(DeepPartial<Book> & Book)[]>', gave the following error.
    Argument of type 'CreateBookDto' is not assignable to parameter of type 'DeepPartial<Book>[]'.
      Type 'CreateBookDto' is missing the following properties from type 'DeepPartial<Book>[]': length, pop, push, concat, and 29 more.
  Overload 2 of 4, '(entity: DeepPartial<Book>, options?: SaveOptions | undefined): Promise<DeepPartial<Book> & Book>', gave the following error.
    Argument of type 'CreateBookDto' is not assignable to parameter of type 'DeepPartial<Book>'.
      Type 'CreateBookDto' is not assignable to type '{ id?: number | undefined; title?: string | undefined; author?: DeepPartial<Author> | undefined; summary?: string | undefined; isbn?: string | undefined; }'.
        Types of property 'author' are incompatible.
          Type 'number' is not assignable to type 'DeepPartial<Author> | undefined'.

如果我这样做,错误就会消失:

create(createBookDto: CreateBookDto) {
  const book = Object.assign(new Book(), createBookDto)
  return this.bookRepository.save(book);
}

但是,我不确定这是否是创建某些东西的正确方法。我还没见过这样的模式。

我该怎么办?创建新记录时通常如何处理关系?也许

author: number
中的
CreateBookDto
有问题?

node.js postgresql nestjs typeorm nestjs-typeorm
1个回答
0
投票

Typeorm 关系 - 按 id 保存

author: 1 as any

尝试作者这样

© www.soinside.com 2019 - 2024. All rights reserved.