#EntityMetadataNotFoundError - cant create repository

145 messages Β· Page 1 of 1 (latest)

restive siren
#

EntityMetadataNotFoundError - cant create repository

harsh jacinth
#

@restive siren Can you send the piece of code where you adjust your data source configuration?

restive siren
#

what do you mean by data source configuration? πŸ˜…

#

i usually add the entity on the module i am working with and add it in a dir that is in the pattern told to orm

#

i can share my main module conf

#
import { Module } from '@nestjs/common';
import { CommonModule } from '@app/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from '@app/auth';
import { UploadsModule } from 'apps/uploads/src/uploads.module';
import { UsersModule } from 'apps/users/src/users.module';
import { CompaniesModule } from 'apps/companies/src/companies.module';
import { PaymentsModule } from 'apps/payments/src/payments.module';
import { ConfigModule } from '@nestjs/config';
import { ReportsModule } from 'apps/reports/src/reports.module';
import { JobsModule } from '@app/jobs';
import { MailingModule } from 'apps/mailing/src/mailing.module';
import { BankingModule } from 'apps/banking/src/banking.module';
require('dotenv').config();

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `${process.cwd()}/.env`,
    }),
    CommonModule,
    AuthModule,
    UsersModule,
    CompaniesModule,
    PaymentsModule,
    UploadsModule,
    ReportsModule,
    MailingModule,
    BankingModule,
    JobsModule,
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: process.env.POSTGRES_HOST,
      port: 5433,
      username: process.env.POSTGRES_USER,
      password: process.env.POSTGRES_PASSWORD,
      database: process.env.POSTGRES_DB,
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      autoLoadEntities: true,
      synchronize: true,
      logging: true, 
    }),
  ],
})
export class AppModule { }
#

and i have adde the new entity in the same dir of other entities that works so is not a path problem,

#

the thing is that the table gets correctly created, but then the repository gives me the error to find the metadata

#

but the table gets created so the metadata shouldnt be known?

harsh jacinth
#

Personally, I don't add the entities option when I set autoLoadEntities to true, and it works. Maybe you can try this

#

And just in case, check if your Requirement entity files are finished with ".entity.ts" at the end

restive siren
#

will try and let you know, thx

restive siren
#

still not working even as you suggested 😒

#

been stuck at this for a week now

spiral fossil
spiral fossil
harsh jacinth
#

@restive siren Does it work for other entities? Or is it the first one you added?

spiral fossil
#

Not the entities per se, but the path.

harsh jacinth
#

Yes that's what I mean

restive siren
#

it does work for the other entities

#

i added a new entity in the same dir of the other entities that already work

#

this is my app module


import { Module } from '@nestjs/common';
import { CommonModule } from '@app/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from '@app/auth';
import { UploadsModule } from 'apps/uploads/src/uploads.module';
import { UsersModule } from 'apps/users/src/users.module';
import { CompaniesModule } from 'apps/companies/src/companies.module';
import { PaymentsModule } from 'apps/payments/src/payments.module';
import { ConfigModule } from '@nestjs/config';
import { ReportsModule } from 'apps/reports/src/reports.module';
import { JobsModule } from '@app/jobs';
import { MailingModule } from 'apps/mailing/src/mailing.module';
import { BankingModule } from 'apps/banking/src/banking.module';
require('dotenv').config();

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `${process.cwd()}/.env`,
    }),
    CommonModule,
    AuthModule,
    UsersModule,
    CompaniesModule,
    PaymentsModule,
    UploadsModule,
    ReportsModule,
    MailingModule,
    BankingModule,
    JobsModule,
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: process.env.POSTGRES_HOST,
      port: 5433,
      username: process.env.POSTGRES_USER,
      password: process.env.POSTGRES_PASSWORD,
      database: process.env.POSTGRES_DB,
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      autoLoadEntities: true,
      logging: true,
    }),
  ],
})
export class AppModule { }
#

the new entity in the same dir as other entities, that correctly works @spiral fossil

spiral fossil
#

How did you set the entity file name?
Make sure it is like name.entity.ts, and it is inside a folder.

#

cuz, you have to make it match the folder pattern /**/.entity.ts as seen in your config.

#

Can you share your file structure ?

restive siren
#
programs/
β”œβ”€β”€ src
β”‚Β Β  β”œβ”€β”€ bounties.entity.ts
β”‚Β Β  β”œβ”€β”€ dto
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ invitation.create.dto.ts
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ program.create.dto.ts
β”‚Β Β  β”‚Β Β  └── program.update.dto.ts
β”‚Β Β  β”œβ”€β”€ index.ts
β”‚Β Β  β”œβ”€β”€ programs.controller.spec.ts
β”‚Β Β  β”œβ”€β”€ programs.controller.ts
β”‚Β Β  β”œβ”€β”€ programs.entity.ts
β”‚Β Β  β”œβ”€β”€ programs.module.ts
β”‚Β Β  β”œβ”€β”€ programs.service.spec.ts
β”‚Β Β  β”œβ”€β”€ programs.service.ts
β”‚Β Β  β”œβ”€β”€ requirements.entity.ts
β”‚Β Β  β”œβ”€β”€ scope-assets.entity.ts
β”‚Β Β  └── tag.entity.ts
β”œβ”€β”€ test
β”‚Β Β  β”œβ”€β”€ app.e2e-spec.ts
β”‚Β Β  └── jest-e2e.json
└── tsconfig.lib.json

4 directories, 17 files

#

the problematic one is the requirements.entity.ts

restive siren
#

just to clarify that the table gets actually created on the database

#

but cant use the repository to interact with it

spiral fossil
#

and.. how do you run migrations?

spiral fossil
#

@restive siren plz lmk if you're able to fix the problem πŸ™

harsh jacinth
#

@restive siren are your requirements entities used on other entities as relations?

restive siren
#

Requirement is not used as a relation as of now @harsh jacinth

#

@spiral fossil I am not running migration for adding an entity, should I? I never done it

spiral fossil
spiral fossil
spiral fossil
#

Also, please check if it doesn't have a relationship with other entities.
If it has, dont forget to include the @JoinColumn() in appropriate place

restive siren
restive siren
spiral fossil
#

is the code publicly available?

#

I'll like to see it.

restive siren
#

no the code is private, company code, i can share snippets if needed, but not the hole code

spiral fossil
#

Okay.
Can you show me how you are injecting it into a class?

restive siren
#

sure

#
import { ForbiddenException, Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Capability } from "apps/companies/src/capabilities.entity";
import { Company } from "apps/companies/src/companies.entity";
import { User } from "apps/users/src/users.entity";
import { Direction, Invitation } from "libs/invitations/src";
import { Profile } from "libs/profile/src";
import { In, Repository } from "typeorm";
import { Severity } from "./bounties.entity";
import { ProgramCreateDto } from "./dto/program.create.dto";
import { ProgramUpdateDto } from "./dto/program.update.dto";
import { Program } from "./programs.entity";
import { Requirement } from "./requirements.entity";
import { ScopeAsset } from "./scope-assets.entity";
@Injectable()
export class ProgramsService {
  constructor(
    @InjectRepository(Program)
    private readonly programRepository: Repository<Program>,
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
    @InjectRepository(Profile)
    private readonly profileRepository: Repository<Profile>,
    @InjectRepository(Company)
    private readonly companyRepository: Repository<Company>,
    @InjectRepository(Invitation)
    private readonly invitationRepository: Repository<Invitation>,
    @InjectRepository(Capability)
    private readonly capabilityRepository: Repository<Capability>,
    @InjectRepository(Requirement)
    private readonly requirementRepository: Repository<Requirement>
  ) {}

  async test(){
    const req = new Requirement();
    req.seats = 6; 
    req.points = 40;
    return this.requirementRepository.save(req);
  }


....
#

it crashes on the line

    return this.requirementRepository.save(req);
#

btw really appreciate your help

spiral fossil
#

i.e await this.requirementRepository.save(req)
also, why are you creating it the old way? 🀣

don't you know how to use this.repo.create? the es6 pattern there is preferrable overtime.

spiral fossil
restive siren
#

added the await you right, but it was just a test func didnt care to wait for that, not solving the error tho

restive siren
#

have a reference about this pattern?

#

like a link or some?

spiral fossil
#

let me check.

restive siren
#

thx

spiral fossil
restive siren
#

what is the repo object

spiral fossil
#

the things inside the entity

#

dont you get a hint from typescript? πŸ˜‰

restive siren
#

sorry, i am not getting how you should create it and where

restive siren
#

dont setted up the best lsp for ts

#

do you have a snippet of code of the pattern to create the repo as you suggested?

spiral fossil
restive siren
#

sure mate

#

whenever you want

spiral fossil
restive siren
#

thx

spiral fossil
#

btw, i'm hacking on an alternative to linear app with Next js and Nest js πŸ‘€

restive siren
#

sorry mate

#

but isnt the same way i am doing it?

#
  constructor(@InjectRepository(User) private repo: Repository<User>) {}
#
    @InjectRepository(Requirement)
    private readonly requirementRepository: Repository<Requirement>
#

aint them the same?

#

it s just the name that names that differs

spiral fossil
#

they're samish

#

try without readonly

spiral fossil
restive siren
#

dont change nothing w/o readonly

i ll send you the full error

EntityMetadataNotFoundError: No metadata for "Requirement" was found.
    at DataSource.getMetadata (/home/murphy/work/cdart/Website-Backend/src/data-source/DataSource.ts:427:30)
    at Repository.get metadata [as metadata] (/home/murphy/work/cdart/Website-Backend/src/repository/Repository.ts:52:40)
    at Repository.save (/home/murphy/work/cdart/Website-Backend/src/repository/Repository.ts:205:18)
    at ProgramsService.test (/home/murphy/work/cdart/Website-Backend/libs/programs/src/programs.service.ts:38:45)
    at ProgramsController.test (/home/murphy/work/cdart/Website-Backend/libs/programs/src/programs.controller.ts:38:33)
    at /home/murphy/work/cdart/Website-Backend/node_modules/@nestjs/core/router/router-execution-context.js:38:29
    at InterceptorsConsumer.transformDeferred (/home/murphy/work/cdart/Website-Backend/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:31:33)
    at /home/murphy/work/cdart/Website-Backend/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:15:53
    at Observable._subscribe (/home/murphy/work/cdart/Website-Backend/node_modules/rxjs/src/internal/observable/defer.ts:55:15)
    at Observable._trySubscribe (/home/murphy/work/cdart/Website-Backend/node_modules/rxjs/src/internal/Observable.ts:244:19)
spiral fossil
#

I think the problem is orm config.

#

have you verified that it has corresponding path to the other entities?

restive siren
#

yes it s in the same path of other entities that already works

.
β”œβ”€β”€ bounties.entity.ts
β”œβ”€β”€ dto
β”‚Β Β  β”œβ”€β”€ invitation.create.dto.ts
β”‚Β Β  β”œβ”€β”€ program.create.dto.ts
β”‚Β Β  └── program.update.dto.ts
β”œβ”€β”€ index.ts
β”œβ”€β”€ programs.controller.spec.ts
β”œβ”€β”€ programs.controller.ts
β”œβ”€β”€ programs.entity.ts
β”œβ”€β”€ programs.module.ts
β”œβ”€β”€ programs.service.spec.ts
β”œβ”€β”€ programs.service.ts
β”œβ”€β”€ requirements.entity.ts
β”œβ”€β”€ scope-assets.entity.ts
└── tag.entity.ts

programs.entity.ts and tag.entity.ts works fine

#

this is my main module where there is my config


import { Module } from '@nestjs/common';
import { CommonModule } from '@app/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from '@app/auth';
import { UploadsModule } from 'apps/uploads/src/uploads.module';
import { UsersModule } from 'apps/users/src/users.module';
import { CompaniesModule } from 'apps/companies/src/companies.module';
import { PaymentsModule } from 'apps/payments/src/payments.module';
import { ConfigModule } from '@nestjs/config';
import { ReportsModule } from 'apps/reports/src/reports.module';
import { JobsModule } from '@app/jobs';
import { MailingModule } from 'apps/mailing/src/mailing.module';
import { BankingModule } from 'apps/banking/src/banking.module';
require('dotenv').config();

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: `${process.cwd()}/.env`,
    }),
    CommonModule,
    AuthModule,
    UsersModule,
    CompaniesModule,
    PaymentsModule,
    UploadsModule,
    ReportsModule,
    MailingModule,
    BankingModule,
    JobsModule,
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: process.env.POSTGRES_HOST,
      port: 5433,
      username: process.env.POSTGRES_USER,
      password: process.env.POSTGRES_PASSWORD,
      database: process.env.POSTGRES_DB,
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      autoLoadEntities: true,
      logging: true,
    }),
  ],
})
export class AppModule { }
#

keep in mind that the table gets actuall created on the db

spiral fossil
#

Okay.
Btw, why arent you using configservice to handle environment variables?

I can imagine that you like less abstractions 🀣

https://docs.nestjs.com/techniques/configuration

restive siren
#

this nestjs code need a lot of review, but as i said i am just helping on it, i am a go boy

#

but thanx for htat

spiral fossil
#

oh... okay.
thinking about the error though

#

can you share what is inside the entity? πŸ™

restive siren
#

but evetho it might not be the best practice it should not be the problem, i guess as the other ones are working

#

sure

spiral fossil
#

yep, just saying!

restive siren
#

.

#

that s the requirement entity

spiral fossil
#

wow, that's too simple to throw an error, lol.

restive siren
#

exactly πŸ˜‚

#

hahahaha

spiral fossil
#

are you connecting to it from two services?

restive siren
#

what you mean?

spiral fossil
#

how many times are you doing TypeormModule.forFeature() on that entity?

restive siren
#

just one, in the module

#
import { Module } from '@nestjs/common';
import { ProgramsService } from './programs.service';
import { ProgramsController } from './programs.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Program } from './programs.entity';
import { ScopeAsset } from './scope-assets.entity';
import { CommonModule } from '@app/common';
import { AuthModule } from '@app/auth';
import { Company } from 'apps/companies/src/companies.entity';
import { Capability } from 'apps/companies/src/capabilities.entity';
import { CaslModule } from '@app/casl';
import { Tag } from './tag.entity';
import { User } from 'apps/users/src/users.entity';
import { Invitation } from 'libs/invitations/src';
import { Profile } from 'libs/profile/src';
import { Bounty } from './bounties.entity';
import { Requirement } from './requirements.entity';

@Module({
  imports: [
    CommonModule,
    AuthModule,
    CaslModule,
    TypeOrmModule.forFeature([Tag, Program, ScopeAsset, Company, Capability, User, Invitation, Profile, Bounty, Requirement])
  ],
  providers: [ProgramsService],
  controllers: [ProgramsController],
  exports: [ProgramsService]
})
export class ProgramsModule { }
spiral fossil
#

I'll get back to you in a bit.
Lemme go do some work πŸ˜„

restive siren
#

oh sure

spiral fossil
#

cool

restive siren
#

hit me up if you have to do some golang πŸ˜‚

#

happy to help back

spiral fossil
#

lol, i never used it, i'll love to learn how to use it.

#

what advice do you have for me?
i'll check them when i come back πŸ˜„

restive siren
#

advices for golang? or what? πŸ˜…

spiral fossil
#

Hey man!

spiral fossil
#

BTW, can you try to create a requirements module, and call TypeormModule.forFeature([Requirements]) there, then you can import the RequirementsModule into the UserModule or Appmodule - whatever you have as the base module.

restive siren
# spiral fossil Yep, howz it better than JS?

they suit for different purposes, i wouldnt say that go is better, they are just done for different reasons, i mean you could do backend in golang, but lets just say that it s not the main purpose, but it s also good for backend dev, i would say less faster than ts framewors tho

spiral fossil
#

Okay, cool.

spiral fossil
restive siren
#

will try in a minute

spiral fossil
#

okay

restive siren
#

working on a golang thing now πŸ˜‚ , just a sec and i am done

spiral fossil
#

cool.

#

let me know πŸ™

spiral fossil
spiral fossil
#

Yoo man.

restive siren
#

sorry man been on work trip

restive siren
#

just managed to set up my stuff back tomorrow will try what you suggested, sorry but really been moving last 24h, sorry had you waiting

spiral fossil
spiral fossil
restive siren
#

ok man i created a new module ill share the files

module:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DickerController } from './dicker.controller';
import { DickerService } from './dicker.service';
import { Dicker } from './test.entity';

@Module({
  imports: [

  TypeOrmModule.forFeature([Dicker])
  ],
  providers: [DickerService],
  controllers: [DickerController],
})
export class DickerModule {}

service:

import { Injectable, NotFoundException, UnauthorizedException } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { Dicker } from "./test.entity";
@Injectable()
export class DickerService {
    constructor(
        @InjectRepository(Dicker)
        private repo: Repository<Dicker>,
    ) { }

    async test(){
      const req = new Dicker();
      req.seats = 6; 
      req.points = 40;
      return await this.repo.save(req);
    }
}

controller:

import { Controller, Get} from "@nestjs/common";
import { DickerService } from "./dicker.service";

@Controller("dicker")
export class DickerController {
  constructor(
    private readonly DickerService: DickerService,
  ) {}

  @Get("test")
  async test(){
    return this.DickerService.test()
  }

}

entity:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Dicker{
    @PrimaryGeneratedColumn("uuid")
    id: string;

    @Column()
    seats: number;

    @Column()
    points: number;
}


#

and i get the same error as before :

[Nest] 63029  - 05/13/2024, 8:41:07 PM   ERROR [ExceptionsHandler] No metadata for "Dicker" was found.
EntityMetadataNotFoundError: No metadata for "Dicker" was found.
    at DataSource.getMetadata (/home/murphy/work/cdart/Website-Backend/src/data-source/DataSource.ts:427:30)
    at Repository.get metadata [as metadata] (/home/murphy/work/cdart/Website-Backend/src/repository/Repository.ts:52:40)
    at Repository.save (/home/murphy/work/cdart/Website-Backend/src/repository/Repository.ts:205:18)
    at DickerService.test (/home/murphy/work/cdart/Website-Backend/apps/dicker/dicker.service.ts:16:30)
    at DickerController.test (/home/murphy/work/cdart/Website-Backend/apps/dicker/dicker.controller.ts:12:31)
    at /home/murphy/work/cdart/Website-Backend/node_modules/@nestjs/core/router/router-execution-context.js:38:29
    at InterceptorsConsumer.transformDeferred (/home/murphy/work/cdart/Website-Backend/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:31:33)
    at /home/murphy/work/cdart/Website-Backend/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:15:53
    at Observable._subscribe (/home/murphy/work/cdart/Website-Backend/node_modules/rxjs/src/internal/observable/defer.ts:55:15)
    at Observable._trySubscribe (/home/murphy/work/cdart/Website-Backend/node_modules/rxjs/src/internal/Observable.ts:244:19)
#

😒 starting to cry on this

spiral fossil
restive siren
#

i dont even know if i have a datasource file

#

the path he give in hte error in the data source do net exist

spiral fossil
#

Can you check what the datsource file is?
That may be the problem

restive siren
#

If that would be the problem why all the rest works

spiral fossil
#

I honestly wish i could make a copy of the codebase πŸ™‚