【问题标题】:TypeScript, TypeORM, @OneToMany: constructor simply does not workTypeScript、TypeORM、@OneToMany:构造函数根本不起作用
【发布时间】:2021-02-06 01:20:03
【问题描述】:

我正在尝试使用 node.js、typescript 和 typeorm 创建两个具有一对多关系的实体。有问题的实体是“Movie”和“MovieVote”。代码如下。

电影实体

import {Column, Entity, JoinColumn, OneToMany, PrimaryGeneratedColumn} from 'typeorm';
import { ApiModel, ApiModelProperty } from 'swagger-express-ts';
import { MovieVote } from "~/entities/MovieVote";
import { Genre } from './Genre';

@Entity()
@ApiModel({
  description: 'The movie entity.',
  name: 'Movie',
})
export class Movie {
  @PrimaryGeneratedColumn()
  id?: number;

  @Column({ length: 50 })
  @ApiModelProperty({
    description: 'The title of the movie',
  })
  title: string;

  @Column({ length: 250 })
  @ApiModelProperty({
    description: 'The description of the movie',
  })
  description: string;

  @Column({ length: 50 })
  @ApiModelProperty({
    description: 'The director of the movie',
  })
  director: string;

  @Column({ enum: Genre, type: 'enum' })
  @ApiModelProperty({
    description: 'The genre of the movie',
  })
  genre: Genre;

  @OneToMany(() => MovieVote, movieVote => movieVote.movie)
  votes: MovieVote[];

  constructor(title: string, description: string, director: string, genre: Genre) {
    this.title = title;
    this.description = description;
    this.director = director;
    this.genre = genre;
    this.votes = [];
  }
}

MovieVote 实体

import {Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn} from 'typeorm';
import { ApiModel, ApiModelProperty } from 'swagger-express-ts';
import { User } from "~/entities/User";
import { Movie } from "~/entities/Movie";

@Entity()
@ApiModel({
  description: 'The movie vote entity.',
  name: 'MovieVote',
})
export class MovieVote {
  @PrimaryGeneratedColumn()
  id?: number;

  @Column()
  @ApiModelProperty({
    description: 'The vote value'
  })
  value: number;

  @ManyToOne(() => User)
  @ApiModelProperty({
    description: 'The user responsible for the vote'
  })
  user: User;

  @ManyToOne(() => Movie, movie => movie.votes)
  // @ApiModelProperty({
  //   description: 'The movie the user has voted on'
  // })
  @JoinColumn({name : 'movieId'})
  movie: Movie;

  constructor(user: User, movie: Movie, value: number) {
    this.value = value;
    this.user = user;
    this.movie = movie;
  }
}

当我尝试运行代码时会发生以下错误:

错误启动服务器,错误:数组初始化不允许 实体关系。请删除数组初始化 (= []) “电影#votes”。这是使关系正常工作的 ORM 要求 适当地。有关详细信息,请参阅文档。

问题似乎出在 Movie 实体构造函数内部。我无法用“= [];”初始化实体内部的关系“投票”,但我不知道应该怎么做。此外,typeorm 的文档中基本上没有提及这一点。

为什么在整个文档中没有具有这种类型的 OneToMany 关系的实体构造函数的示例,这超出了我的范围,但我离题了。欢迎任何帮助,在此先感谢您!

P.S.:我已经在 MovieVote 上评论了 @ApiModelProperty 注释,因为由于未知原因,这也会因“_decorator 错误”以及与未定义属性相关的内容而崩溃。

【问题讨论】:

    标签: node.js typescript typeorm


    【解决方案1】:

    问题是this.votes = []

    通常,实体的构造函数不会传入任何值,也不应设置任何值。 TypeORM 将为您处理创建所有内容。

    如果您需要能够使用new 构建您自己的类实例,您可以执行我所做的类似操作并更改为以下内容。

      constructor(title?: string, description?: string, director?: string, genre?: Genre) {
        if (title) {
          this.title = title;
          this.description = description;
          this.director = director;
          this.genre = genre;
          this.votes = [];
        }
      }
    
    

    【讨论】:

    • 如果我在关注你,这相当于为 typeorm 使用的类提供一个 0-arg CTOR,以及一个为用户代码使用 args 的“普通”CTOR。
    猜你喜欢
    • 2022-12-18
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    相关资源
    最近更新 更多