错误:添加多对多关系后找不到对象元数据

时间:2019-12-24 12:19:22

标签: typeorm

我需要具有用于多对多关系的其他属性,我自己创建了一个新实体,如文档所示。

https://github.com/typeorm/typeorm/blob/master/docs/many-to-many-relations.md#many-to-many-relations-with-custom-properties

它是用户实体。

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { ShareTask } from './../../share-taks/entity/share-taks.entity';

@Entity('users')
export class User {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({ type: 'varchar', nullable: false })
    public email: string;

    @Column({ type: 'varchar', nullable: false })
    public password: string;

    @OneToMany(() => ShareTask, (shareTask: ShareTask) => shareTask.user)
    public shareTask: ShareTask[];
}

它是任务实体。

import { Entity, Column, PrimaryGeneratedColumn, OneToMany } from 'typeorm';
import { ShareTaks } from './../../share-taks/entity/share-taks.entity';

@Entity('tasks')
export class Taks {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({ type: 'varchar', nullable: false, length: 50 })
    public title: string;

    @OneToMany(() => ShareTaks, (shareTaks: ShareTaks) => shareTaks.taks)
    public shareTaks: ShareTaks[];    
}

它是ormconfig.json。

{
    "type": "mysql",
    "host": "localhost",
    "port": 3306,
    "username": "root",
    "password": "7047",
    "database": "db",
    "entities": ["dist/**/*.entity{.ts,.js}"],
    "synchronize": true
}

这是ShareTaks实体。

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

@Entity('shareTasks')
export class shareTask {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({ type: 'integer', nullable: false })
    public taskId: number;

    @Column({ type: 'integer', nullable: false })
    public userId: number;

    @ManyToOne(() => (task: Task) => task.shareTask, { cascade: ['insert', 'update', 'remove'] })
    public task: Task;

    @ManyToOne(() => (user: User) => user.shareTask, { cascade: ['insert', 'update', 'remove'] })
    public user: User;
}

在我建立多对多关系之前,一切正常。添加多对多关系时,出现错误。

  

错误:找不到ShareTask#task的实体元数据。检查是否在连接选项中指定了正确的实体对象,并且该对象是否已连接。

由于出现错误,我无法理解。

我找到了我问题的一些答案,但是它们并没有帮助我。

Entity metadata for Role#users was not found

https://github.com/typeorm/typeorm/issues/420

1 个答案:

答案 0 :(得分:1)

同样,该错误是由您在ormconfig文件中的实体中提供的路径引起的。尝试将其更改为当前目录的相对路径,例如

"entities": [__dirname + "/../**/*.entity{.ts,.js}"]