TypeORM-joinColumns的Getter&Setter问题-无法读取未定义的属性'joinColumns'

时间:2018-12-20 05:07:55

标签: typeorm

我创建了两个具有关联关系的用户和照片实体。当我尝试查找与用户的关系的所有数据时,出现此错误:

TypeError:无法读取未定义的属性'joinColumns'

我删除Getter&Setter并将属性设置为公共访问权限并删除下划线后,它就可以正常工作。但是我实际上想保留Getters&Setters。

user.entity.ts

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { Photo } from './photo.entity';

@Entity()
export class User {
  @PrimaryGeneratedColumn({ name: 'id' })
  private _id: number;

  @Column({ name: 'name' })
  private _name: string;

  @OneToMany(type => Photo, photo => photo.user)
  private _photos: Photo[];

  public get id(): number {
    return this._id;
  }

  public set id(value: number) {
    this._id = value;
  }

  public get name(): string {
    return this._name;
  }

  public set name(value: string) {
    this._name = value;
  }

  public get photos(): Photo[] {
    return this._photos;
  }

  public set photos(value: Photo[]) {
    this._photos = value;
  }
}

photo.entity.ts

import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { User } from './user.entity';

@Entity()
export class Photo {
  @PrimaryGeneratedColumn({ name: 'id' })
  private _id: number;

  @Column({ name: 'url' })
  private _url: string;

  @ManyToOne(type => User, user => user.photos)
  private _user: User;

  public get id(): number {
    return this._id;
  }

  public set id(value: number) {
    this._id = value;
  }

  public get url(): string {
    return this._url;
  }

  public set url(value: string) {
    this._url = value;
  }

  public get user(): User {
    return this._user;
  }

  public set user(value: User) {
    this._user = value;
  }
}

user.repository.ts

public async findUsers(): Promise<User[]> {
  return await this.find({ relations: ['_photos'] }); // photos (without _) isn't working as well
}

0 个答案:

没有答案