question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Entity Metadata Not Found: No metadata for "User" was found.

See original GitHub issue

Issue type:

[ ] question [X] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I’m having a similar issue covered in #420 however the solution did not apply here.

// Database.ts
import { createConnection } from "typeorm";
import { User } from "../app/models/User.model";
import { config, DIALECT } from "../config";

export const Connection = createConnection({
  database: config.DATABASE.DB,
  entities: [
    User
  ],
  host: config.DATABASE.SERVER,
  logging: true,
  password: config.DATABASE.PASSWORD,
  port: config.DATABASE.PORT_DB,
  synchronize: true,
  type: DIALECT,
  username: config.DATABASE.USER_DB,
});
// User.model.ts
import { BaseEntity, BeforeInsert, Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { Contains, IsInt, IsString, Length, IsEmail, IsFQDN, IsDate, Min, Max, IsBoolean } from "class-validator";
import * as bcrypt from 'bcrypt';

@Entity("user")
export class User extends BaseEntity {

  @PrimaryGeneratedColumn()
  public id: number;

  @Column()
  @IsEmail()
  public email: string
  
  @Column()
  @IsString()
  public password: string

  @Column({
    type: "enum",
    enum: [
      "student",
      "tutor"
    ]
  })
  public type: string

  @Column()
  @IsString()
  public firstName: string;

  @Column()
  @IsString()
  public lastName: string;

  @Column({
    type: "enum",
    enum: [
      "Male",
      "Female",
      "Other"
    ]
  })
  @IsString()
  public gender: string;

  @Column()
  @IsString()
  public phone: string;

  @Column()
  @IsBoolean()
  public verified: boolean;

  @BeforeInsert()
  encryptPassword() {
    this.password = bcrypt.hashSync(this.password, 10);
  }
}
// User.repository.ts
import { EntityRepository, Repository } from "typeorm";
import { User } from "../models/User.model";

@EntityRepository(User)
export class UserRepository extends Repository<User> {
}
// User.controller.ts
export class UserController {
    public static userRepository: UserRepository = getCustomRepository(UserRepository);

    constructor() {
    }
}

This is the error with that is thrown:

/Users/john/Documents/Work/PROJECT/src/error/EntityMetadataNotFoundError.ts:9
        super();
        ^
EntityMetadataNotFound: No metadata for "User" was found.
    at new EntityMetadataNotFoundError (/Users/john/Documents/Work/PROJECT/src/error/EntityMetadataNotFoundError.ts:9:9)
    at Connection.getMetadata (/Users/john/Documents/Work/PROJECT/src/connection/Connection.ts:313:19)
    at EntityManager.getCustomRepository (/Users/john/Documents/Work/PROJECT/src/entity-manager/EntityManager.ts:753:86)
    at Connection.getCustomRepository (/Users/john/Documents/Work/PROJECT/src/connection/Connection.ts:348:29)
    at Object.getCustomRepository (/Users/john/Documents/Work/PROJECT/src/index.ts:286:55)
    at Object.<anonymous> (/Users/john/Documents/Work/PROJECT/app/controllers/User.controller.ts:9:52)
    at Module._compile (module.js:652:30)
    at Module.m._compile (/Users/john/Documents/Work/PROJECT/node_modules/ts-node/src/index.ts:400:23)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/john/Documents/Work/PROJECT/node_modules/ts-node/src/index.ts:403:12)

It seems getCustomRepository(UserRepository); is what is causing the error, and it can’t get User’s metadata.

In issue #420 it seems like the solution was to fix all import statements for the entity, and I’ve done that. Another proposed solution is to make sure your entity is listed in your database connection, and it is.

I’m new to typeorm, however I’m really liking it compared to Sequelize, I love how typeorm integrates with TypeScript with such ease. I’m prototyping both right now for a new project and I really don’t want to have to use Sequelize. 😀

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
johnpanoscommented, Jun 3, 2018

Nevermind, I figured it out, it turns out in my index.ts file I imported my route.ts file, and the route instantiated the UserController before the connection was made, so the user entity didn’t exist in the connection, because the connection wasn’t made yet.

So I just made my route configuration a function that I called after I connected, and all works well!

I’ll close this now, gosh I feel stupid haha. Great project you guys have here.

1reaction
jmcruzmanalocommented, Sep 9, 2019

@havenchyk In this case, I believe class-validator is used at run-time together with ValidationPipe. Basically it’s a way to check if the data sent to the route is a string for that specific property.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity metadata for Role#users was not found - Stack Overflow
I am using NestJS with PostgreSQL and I had the same issue. The problem was by me is that I forgot to import...
Read more >
Error: Entity metadata was not found · Issue #420 - GitHub
Conclusion: this error happening when you have misspelled your entity name, misspelled import paths or did not include your entity in connection ...
Read more >
no metadata for was found. - You.com | The AI Search Engine ...
Solution for EntityMetadataNotFound: No metadata for was found The entity is a Typescript class that maps to a table in the database. If...
Read more >
TypeORM - No metadata for "User" was found. - Reddit
TypeORM : EntityMetadataNotFound: No metadata for "User" was found. Hey all! Hoping some typeORM experts can help me out, burned wayyy to much ......
Read more >
typeorm/typeorm - Gitter
I feel like I'm close but I get the error: TypeORM connection error: Error: Entity metadata for Toy#creator was not found. Check if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found