Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM cannot find entities if entity directory was not set in configuration files

I'm using TypeORM with the fallowing configuration file: ormconfig.json

{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "my-secret-pw",
"database": "mytestdb",
}

My Entities files are stored on the ./src/bar/entity directory. I always get the following error:

RepositoryNotFoundError: No repository for "myTable" was found. Looks like this entity is not registered in current "default" connection?

The Entity is found when I manually add the directory to the configuration file:

{
...
"entities": ["src/bar/entity/**/*.ts"]
}

My Entity is defined like:

@Entity('myTable')
export default class MyTable {
    @PrimaryGeneratedColumn()
    public id: number;
    ...

How can I allow the TypeORM to find those entities without setting manually in the configuration file for each directory?

like image 542
Daniel Santos Avatar asked Sep 03 '18 19:09

Daniel Santos


3 Answers

The most common case you described is to have separate entities directory which consists only of Entity declarations.

{
...
"entities": ["src/bar/entities/**/*.ts"]
}

Another approach would be importing each entity separately:

import {User} from "./payment/entity/User";
import {Post} from "./blog/entity/Post";

{
...
"entities": [User, Post]
}
like image 193
Risto Novik Avatar answered Nov 08 '22 18:11

Risto Novik


For me it helped to include also src directory to ormconfig.json:

  "entities": [
    "dist/**/*.entity{.ts,.js}",
    "src/**/*.entity{.ts,.js}"
  ],
like image 24
michal.jakubeczy Avatar answered Nov 08 '22 18:11

michal.jakubeczy


For me the answer was { ... entities: [join(__dirname, '/../**/**.entity{.ts,.js}')], }

I've found the exapmle here https://github.com/nestjs/nest/blob/master/sample/05-sql-typeorm/src/app.module.ts

like image 9
Victor Avatar answered Nov 08 '22 18:11

Victor