E
E
eugene none2021-05-10 12:31:41
PostgreSQL
eugene none, 2021-05-10 12:31:41

Nest js, TypeORM many-to-many, why can't update?

I'm having trouble updating an entity that has manytomany.

Following are the Products and Category entities .

@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 in service.

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);
  }


I don’t understand what exactly the problem is, since the creation works, there is data in the database, I send an update request with data similar to how I created it.
Below is a JSON example of how I send data.

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


The error that comes out.

[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)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question