javascript - Typeorm 中的关系
问题描述
我有一个数据库,其结构如下所示:
类别表
import { productsEntity } from "src/products/entity/products.entity";
import { BaseEntity, Column, Entity, ManyToMany, PrimaryGeneratedColumn } from "typeorm";
@Entity('categories')
export class category extends BaseEntity{
@PrimaryGeneratedColumn()
id: number;
@Column({nullable: false, default: 'all', unique: true})
title: string;
@ManyToMany(()=> productsEntity, product => product.categories)
products: productsEntity[]
}
产品表
import { category } from "src/products/entity/category.entity";
import { BaseEntity, Column, CreateDateColumn, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";
@Entity('products')
export class productsEntity extends BaseEntity{
@PrimaryGeneratedColumn()
id: number;
//.... Columns
@ManyToMany(() => category, categories => categories.products, {cascade: true})
@JoinTable({name: 'product_category'})
categories: category[];
}
这两个表都具有多对多关系。当我创建这样的新产品时:
async addProduct(productDetails){
const {title, description, belongsTo, price, units} = productDetails;
let newProduct = new productsEntity();
newProduct.title = title;
newProduct.description = description;
newProduct.price = price;
newProduct.units = units;
newProduct.categories = [belongsTo];
newProduct.save();
}
}
它保存运行此查询的所有字段:
INSERT INTO "products"("title", "description", "price", "units", "created_at", "updated_at") VALUES ($1, $2, $3, $4, DEFAULT, DEFAULT) RETURNING "id", "created_at", "updated_at"
但不是在类别中,新的中间表也没有更新。我究竟做错了什么??
解决方案
推荐阅读
- html - 如何永久隐藏菜单项?
- list - 优化从带有文件夹的库中下载文件的脚本
- confluent-schema-registry - 如何删除主题级别的兼容性级别
- r - r中的滚动标准偏差
- python - TypeError:调用 print 时,'str' 对象不可调用
- loopbackjs - 如何在 LoopBack 4 中直接添加控制器?
- javascript - Highcharts - 如何获得点击事件的最近点
- python - 将文件转换为灰度图像
- c# - AWS CDK - 如何修复“Fatal process OOM in enough memory to create an Isolate”错误?
- python - venv 不尊重 --copies 参数