【Koa2】typeorm No repository for “XXX“ was found. Looks like this entity is not registered...

写在前面

Today,在开发环境可以正常运行 typeorm 并且找到实体类,但是一打包生产环境用 pm2 运行就不行了,报错如下:

No repository for “UserInfo” was found. Looks like this entity is not
registered in current “default” connection

在这里插入图片描述

先看看是不是实体类上面没有加 @Entity 注解!
如下所示

/**
 * AvatarInfo Entity
 */
import { Entity, Column } from 'typeorm';
import { BaseEntity } from './BaseEntity';

@Entity({ name: 'tb_avatar_info' }) // <<< === 切记加上注解!
export class AvatarInfo extends BaseEntity {
  @Column({ name: 'user_id' })
  userId: string;

  @Column({ name: 'file_id' })
  fileId: string;

  @Column({ name: 'status' })
  status: number;
}

解决方案

  1. 首先观察我的项目目录结构
    如下图,生成后的 dist 位于根目录下,所以 build 的 时候 entities 的位置需要从根目录开始拼写!
    项目结构

  2. 找到项目下的 ormconfig.js,作如下修改(如果你是 json,请改成 js)

const env = process.env.NODE_ENV;

module.exports = {
  type: 'mysql',
  host: 'your database ip',
  port: your_port,
  username: 'your_username',
  password: 'your_password',
  database: 'your_database',
  synchronize: false,
  // 根据你实体类文件夹实际地址填写
  entities: [`${env === 'development' ? 'src/' : 'dist/src/'}entity/*{.js,.ts}`]
};

  1. 重新打包并运行,解决!

项目地址

  1. Koa + Typescript + typeorm 起手架子:https://github.com/supervons/PreheatingKoa
  2. 示例轮子项目:https://github.com/supervons/ExploreKoa
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值