postgresql - (Typeorm/Postgres)从关系实体(产品)保存树实体(类别)导致错误
问题描述
使用类别名称创建产品,返回 MaterializedPathSubjectExecutor 错误。
创建类别作品。但是尝试使用关系类别保存产品会引发错误。
尝试添加到 @OneToMany()
{
persistence:false, cascade:false, eager:true
}
还在关系的两侧尝试了@JoinColumn()
类别检索功能
async getCategories(category: string) {
const manager = getManager();
const cat = await manager
.getRepository(Category)
.findOne({
name: Like(category),
})
.catch((error) => {
console.error(error);
});
console.log(cat);
if (!cat) {
const cat = new Category();
cat.name = category;
cat.description = category;
return await manager.getRepository(Category).save(cat);
}
return cat;
}
保存功能
async create(createdBy: string, productDto: CreateProductDto) {
const { name, price, isActive, description, category, images } = productDto;
const product = new Product();
product.name = name;
product.price = price;
product.isActive = !!isActive;
const creator = await this.usersService.findForId(createdBy);
product.createdBy = creator;
product.updatedBy = creator;
product.description = description;
product.images = await this.imageService.upload(images);
if (category) {
console.log('WHAT');
product.category = await this.getCategories(category);
}
return await this.productsRepository.save(product).catch((err) => {
console.error(err);
console.log(product);
});
产品实体.ts
import { Image } from 'src/modules/image/entity/image.entity';
import { User } from 'src/modules/user/user.entity';
import { BaseEntity } from 'src/utility/entity/base.entity';
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
Tree,
} from 'typeorm';
@Entity()
@Tree('materialized-path')
export class Product extends BaseEntity {
@Column()
name: string;
@Column()
price: number;
@Column({ type: 'varchar', length: 300 })
description: string;
@OneToMany(() => Image, (image) => image.product)
images: Image[];
@ManyToOne((type) => Category, (category) => category.products)
category: Category;
@ManyToOne((type) => User, (user) => user.products_created, { lazy: true })
@JoinColumn()
createdBy: User;
@ManyToOne((type) => User, (user) => user.products_created, { lazy: true })
@JoinColumn()
updatedBy: User;
}
类别实体.ts
@Entity({ name: 'category' })
@Tree('materialized-path')
export class Category extends BaseEntity {
@Column({ type: 'varchar', length: 300 })
/**
* Name of category
* @example 'book'
* */
name: string;
@Column({ type: 'varchar', length: 300 })
description: string;
@TreeChildren()
children: Category[];
@TreeParent()
parent: Category;
@OneToMany((type) => Product, (product) => product.category)
products: Product[];
@ManyToOne((type) => User, (user) => user.category_created, { lazy: true })
@JoinColumn()
createdBy: User;
@ManyToOne((type) => User, (user) => user.category_updated, { lazy: true })
@JoinColumn()
updatedBy: User;
}
错误:已生成
TypeError: Cannot read property 'getEntityValue' of undefined
at MaterializedPathSubjectExecutor.<anonymous>
解决方案
推荐阅读
- django - Divio App 麻烦创建项目目录和克隆存储库
- entity-framework - 第一次在运行时应用迁移时忽略架构
- java - 有选择地为文档中的某些区域停用 jsoup 中的漂亮打印
- python - 如何在python中增加尺寸并提高图像的质量?
- nginx - nginx:[emerg] 未知指令“gop_cache”
- sql-server - 使用 pandas.Dataframe.to_sql 和 sqlalchemy 的连接问题?
- unity3d - 如何将文件从手机上传到我的 Amazon S3 服务器?
- javascript - 从反应原生的构造函数调用时,navigation.getParam 不起作用
- facebook-graph-api - 使用 facebook graph api 读取照片标签
- asp.net - 如何确定 XML 配置中的文件路径