typeorm - 找不到 Role#users 的实体元数据
问题描述
尝试OneToMany
与ManyToOne
TypeORM 建立关系,但我收到此错误,我不知道我的代码有什么问题。
我有以下用户实体:
import { BaseEntity, Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Field, ID, ObjectType } from 'type-graphql';
import { Role } from './';
@ObjectType()
@Entity()
export class User extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
public id: number;
@Field()
@Column('text', { unique: true })
public userName: string;
@Column()
public password: string;
@Field()
@Column('boolean', { default: true })
public isActive: boolean;
@ManyToOne(() => Role, role => role.users)
@Field(() => Role, { nullable: true })
public role: Role;
}
角色实体:
import { BaseEntity, Column, Entity, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { Field, ID, ObjectType } from 'type-graphql';
import { User } from '.';
@ObjectType()
@Entity()
export class Role extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
public id: number;
@Field()
@Column('text', { unique: true })
public name: string;
@OneToMany(() => User, user => user.role, { lazy: false })
@Field(() => [User], { nullable: true })
public users: User[];
}
但是我不断收到此错误
(node:4541) UnhandledPromiseRejectionWarning: Error: Entity metadata
for Role#users was not found. Check if you specified a correct entity
object and if it's connected in the connection options. [1] at
/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:571:23
[1] at Array.forEach (<anonymous>) [1] at
EntityMetadataBuilder.computeInverseProperties
(/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:567:34)
[1] at
/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:80:74
[1] at Array.forEach (<anonymous>) [1] at
EntityMetadataBuilder.build
(/node_modules/typeorm/metadata-builder/EntityMetadataBuilder.js:80:25)
[1] at ConnectionMetadataBuilder.buildEntityMetadatas
(/node_modules/typeorm/connection/ConnectionMetadataBuilder.js:57:141)
[1] at Connection.buildMetadatas
(/node_modules/typeorm/connection/Connection.js:494:57)
[1] at Connection.<anonymous>
(/node_modules/typeorm/connection/Connection.js:126:30)
[1] at step
(/node_modules/tslib/tslib.js:136:27) [1]
(node:4541) UnhandledPromiseRejectionWarning: Unhandled promise
rejection. This error originated either by throwing inside of an async
function without a catch block, or by rejecting a promise which was
not handled with .catch(). (rejection id: 1) [1] (node:4541) [DEP0018]
DeprecationWarning: Unhandled promise rejections are deprecated. In
the future, promise rejections that are not handled will terminate the
Node.js process with a non-zero exit code.
解决方案
我将NestJS与 PostgreSQL 一起使用,我遇到了同样的问题。问题出在我身上,我忘记在模块中使用该TypeOrmModule.forFeature
功能导入实体。
@Module({
imports: [TypeOrmModule.forFeature([Users, ...])], <--- Importing the entity!
controllers: [...],
providers: [...],
exports: [...],
})
推荐阅读
- ios - 找不到“字符串”文件 - Firebase 更新和 leveldb-library 的问题
- javascript - 我的 reducer 可以使用 Set 作为初始值吗?
- elasticsearch - Elasticsearch 如何计算此函数分数查询中的分数?奇怪的行为
- firebase - 更改 android 的帐户 firebase
- java - 使用 Jersey 2 的 REST(在使用 Guice 的 GAE java 应用程序上)在服务器上不起作用
- r - R中字母之间所有可能的点组合
- javascript - 根据第一列中选择的内容启用第二列中的单选按钮
- azure-webjobs - Azure LogicApps 可以停止网络作业吗?
- sql - 如何在临时 DataTable 中执行循环并在比较两个表之间的值后更新 SQL 表
- android - 在平面图中上下滑动