Answer the question
In order to leave comments, you need to log in
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[];
}
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);
}
{
"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)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question