【第六章】数据关联与树形嵌套
http://localhost:3333/courses/nestjs/chapter6
![[upl-image-preview uuid=36ca1493-bb69-46d6-8b0e-3d44410b5d98 url=https://cn-nb1.rains3.com/3rcd/2024-01-31/1706680604-84153-image.png fullscreen_uri={URL}]
](https://)
在进行模型编写后,再启动项目,一直显示错误。
[upl-image-preview uuid=09d83b00-3faa-42d7-96c1-88b754a965b1 url=https://cn-nb1.rains3.com/3rcd/2024-01-31/1706680668-974787-image.png fullscreen_uri={URL}]
目前尝试的方法有pnpm rebuild,无效。也去stack over flow找过问题,但是似乎不想关。
import { Expose } from "class-transformer";
import { Entity,
BaseEntity,
PrimaryColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
ManyToOne,
Relation,
JoinTable,
ManyToMany,
OneToMany} from "typeorm";
import { PostBodyType } from "../constants";
import { CategoryEntity } from "./category.entity";
import { CommentEntity } from "./comment.entity";
import { TagEntity } from "./tag.entity";
// src/modules/content/entities/post.entity.ts
@Entity('content_posts')
export class PostEntity extends BaseEntity {
@Expose()
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
id: string;
@Expose()
@Column({ comment: '文章标题' })
title: string;
@Expose({ groups: ['post-detail'] })
@Column({ comment: '文章内容', type: 'text' })
body: string;
@Expose()
@Column({ comment: '文章描述', nullable: true })
summary?: string;
@Expose()
@Column({ comment: '关键字', type: 'simple-array', nullable: true })
keywords?: string[];
@Expose()
@Column({
comment: '文章类型',
type: 'varchar',
// 如果是mysql或者postgresql你可以使用enum类型
// enum: PostBodyType,
default: PostBodyType.MD,
})
type: PostBodyType;
@Expose()
@Column({
comment: '发布时间',
type: 'varchar',
nullable: true,
})
publishedAt?: Date | null;
@Expose()
@Column({ comment: '自定义文章排序', default: 0 })
customOrder: number;
@Expose()
@CreateDateColumn({
comment: '创建时间',
})
createdAt: Date;
@Expose()
@UpdateDateColumn({
comment: '更新时间',
})
updatedAt: Date;
@Expose()
@ManyToOne(() => CategoryEntity, (category) => category.posts, {
nullable: true,
onDelete: 'SET NULL',
})
category: Relation<CategoryEntity>;
@Expose()
@ManyToMany(() => TagEntity, (tag) => tag.posts, {
cascade: true,
})
@JoinTable()
tags: Relation<TagEntity>[];
@OneToMany(() => CommentEntity, (comment) => comment.post, {
cascade: true,
})
comments: Relation<CommentEntity>[];
}
import { Entity, BaseEntity, PrimaryColumn, Column, OneToMany, Relation } from "typeorm";
import { PostEntity } from "./post.entity";
@Entity('content_categories')
export class CategoryEntity extends BaseEntity {
@PrimaryColumn({ type: 'varchar', generated: 'uuid', length: 36 })
id: string;
@Column({ comment: '分类名称' })
name: string;
@Column({ comment: '分类排序', default: 0 })
customOrder: number;
@OneToMany(() => PostEntity, (post) => post.category, {
cascade: true,
})
posts: Relation<PostEntity[]>;
}
配置如下:
[upl-image-preview uuid=07d5b199-14f6-4427-92b7-f8a0ed81a9d2 url=https://cn-nb1.rains3.com/3rcd/2024-01-31/1706680859-236514-image.png fullscreen_uri={URL}]
[upl-image-preview uuid=ce0009f9-2eed-4d2d-b9cb-4ff56d9e6005 url=https://cn-nb1.rains3.com/3rcd/2024-01-31/1706680904-14224-image.png fullscreen_uri={URL}]
求问下,可能是什么问题,导致框架无法检测到实体的原数据?
@Cloneable