manytone/OneToMany-typeORM-postgresql
所以,我对typeORM和postgres DB有点陌生,我发现有些东西让我有点困惑,但是,在告诉你我的问题之前,让我给你看一些代码 这就是所谓的投票manytone/OneToMany-typeORM-postgresql,postgresql,typeorm,Postgresql,Typeorm,所以,我对typeORM和postgres DB有点陌生,我发现有些东西让我有点困惑,但是,在告诉你我的问题之前,让我给你看一些代码 这就是所谓的投票 import { Entity as TOEntity, Column, JoinColumn, Index, BeforeInsert, ManyToOne } from "typeorm"; import Entity from "./Entity"; import User
import {
Entity as TOEntity,
Column,
JoinColumn,
Index,
BeforeInsert,
ManyToOne
} from "typeorm";
import Entity from "./Entity";
import User from "./User";
import Post from "./Post";
import Comment from "./Comment";
@TOEntity("votes")
export default class Vote extends Entity {
constructor(vote: Partial<Vote>) {
super();
Object.assign(this, vote);
}
@Column()
value: number;
@ManyToOne(() => User)
@JoinColumn({ name: "username", referencedColumnName: "username" })
user: User;
@Column()
username: string;
@ManyToOne(() => Post)
post: Post;
@ManyToOne(() => Comment)
comment: Comment;
}
导入{
实体作为实体,
专栏,
JoinColumn,
指数
在插入之前,
多对一
}从“类型化”;
从“/Entity”导入实体;
从“/User”导入用户;
从“/Post”导入Post;
从“/Comment”导入注释;
@TOEntity(“投票”)
导出默认类投票扩展实体{
建造商(投票:部分){
超级();
反对。分配(本,投票);
}
@第()列
值:数字;
@多通(()=>用户)
@JoinColumn({name:“username”,referencedColumnName:“username”})
用户:用户;
@第()列
用户名:字符串;
@多通(()=>Post)
岗位:岗位;
@多通(()=>评论)
评论:评论;
}
这个叫做评论
import {
Entity as TOEntity,
Column,
ManyToOne,
OneToMany,
JoinColumn,
BeforeInsert,
Index
} from "typeorm";
import Entity from "./Entity";
import User from "./User";
import Post from "./Post";
import Vote from "./Vote";
import { makeid } from "../util/helpers";
@TOEntity("comments")
export default class Comment extends Entity {
constructor(comment: Partial<Comment>) {
super();
Object.assign(this, comment);
}
@Index()
@Column()
identifier: string;
@Column()
body: string;
@Column()
username: string;
@ManyToOne(() => User)
@JoinColumn({ name: "username", referencedColumnName: "username" })
user: User;
@ManyToOne(() => Post, post => post.comments, { nullable: false })
post: Post;
@OneToMany(() => Vote, vote => vote.comment)
votes: Vote[];
protected userVote: number;
setUserVote(user: User) {
const index = this.votes.findIndex(v => v.username === user.username);
this.userVote = index > -1 ? this.votes[index].value : 0;
}
@BeforeInsert()
makeIdAndSlug() {
this.identifier = makeid(8);
}
}
导入{
实体作为实体,
专栏,
许多人,
一家公司,
JoinColumn,
在插入之前,
指数
}从“类型化”;
从“/Entity”导入实体;
从“/User”导入用户;
从“/Post”导入Post;
从“/Vote”导入投票;
从“./util/helpers”导入{makeid};
@TOEntity(“评论”)
导出默认类注释扩展实体{
构造函数(注释:部分){
超级();
分配(此,注释);
}
@索引()
@第()列
标识符:字符串;
@第()列
正文:字符串;
@第()列
用户名:字符串;
@多通(()=>用户)
@JoinColumn({name:“username”,referencedColumnName:“username”})
用户:用户;
@manytone(()=>Post,Post=>Post.comments,{nullable:false})
岗位:岗位;
@OneToMany(()=>Vote,Vote=>Vote.comment)
投票:投票[];
受保护用户投票:数字;
setUserVote(用户:用户){
const index=this.voces.findIndex(v=>v.username===user.username);
this.userVote=index>-1?this.votes[index]。值:0;
}
@插入()之前
makeIdAndSlug(){
this.identifier=makeid(8);
}
}
问题:投票和评论之间有一种关系,问题是在投票实体中我们有一行代码@ManyToOne(()=>comment)comment:comment代码>
在comment实体中,我们有另一行代码@OneToMany(()=>Vote,Vote=>Vote.comment)投票:Vote[]代码>
目标:现在,为什么在投票实体中我们只放()=>评论,而不是()=>评论。投票?我需要了解它和它的功能,如果你能帮我,我会感谢你
谢谢你的时间