@sieugene
Frontend developer

Nest js, TypeORM many-to-many, почему не удается обновить?

У меня возникли проблемы с обновлением сущности, которая имеет manytomany.

Ниже приведены сущности Products и Category .

@Entity()
class Products {
  @PrimaryGeneratedColumn()
  public id: number;

  @Column()
  public title: string;

  @Column()
  public content: string;

  @ManyToMany((type) => Category, (category) => category.products)
  @JoinTable()
  @Type(() => Category)
  categories: Category[];
}


@Entity()
class Category {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @ManyToMany((type) => Products, (products) => products.categories, {
    cascade: true,
  })
  @Type(() => Products)
  products: Products[];
}


Update в сервисе.

async updateProduct(id: number, product: UpdateProductDto) {
    await this.service.update(id, product);
    const updatedProduct = await this.service.findOne(id, {
      relations: ['categories'],
    });
    if (updatedProduct) {
      return updatedProduct;
    }
    throw new ProductNotFoundException(id);
  }


Я не понимаю, в чем именно проблема, так как создание работает, данные в бд есть, я отправляю запрос на update с данными, аналогичными тому, как его создавал.
Ниже JSON пример того как отправляю данные.

{
    "content": "test",
    "title": "test",
    "categories": [
        {
            "id": 3
        }
    ]
}


Ошибка которая выходит.

[ExceptionsHandler] column "productsId" of relation "products" does not exist +23003ms
QueryFailedError: column "productsId" of relation "products" does not exist
at new QueryFailedError (...\node_modules\typeorm\error\QueryFailedError.js:12:28)
at PostgresQueryRunner. (...\node_modules\typeorm\driver\postgres\PostgresQueryRunner.js:248:31)
at step (...\node_modules\typeorm\node_modules\tslib\tslib.js:143:27)
at Object.throw (...\node_modules\typeorm\node_modules\tslib\tslib.js:124:57)
at rejected (...\node_modules\typeorm\node_modules\tslib\tslib.js:115:69)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
  • Вопрос задан
  • 703 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы