#EntityMetadataNotFoundError - cant create repository
145 messages Β· Page 1 of 1 (latest)
@restive siren Can you send the piece of code where you adjust your data source configuration?
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?
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
will try and let you know, thx
Yep, but you have to specify the path to load the entities from, so you must set the entity path
What do you want to solve?
@spiral fossil Are you sΓ»re ? I don't specify the path when I load my entities via the TypeOrmModule.forFeature
typescript TypeOrmModule.forFeature([MyEntity, ...])
@restive siren Does it work for other entities? Or is it the first one you added?
Not the entities per se, but the path.
Yes that's what I mean
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
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 ?
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
all the other entites work perfectly
just to clarify that the table gets actually created on the database
but cant use the repository to interact with it
and.. how do you run migrations?
@restive siren plz lmk if you're able to fix the problem π
@restive siren are your requirements entities used on other entities as relations?
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
Okay, I remembered that you have synchronize from typeorm
So.. what problem do you wanna solve? Do you get the error when you add requirements entity to typeorm?
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
i get an error when trying to use the repository to save/update or stuff with the new entity
no relationships with other entities
no the code is private, company code, i can share snippets if needed, but not the hole code
Okay.
Can you show me how you are injecting it into a class?
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
well, you should await that call.
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.
don't mention, i'll be here to help you until this gets resolved.
that's kinda how i pay back to the communit π
nope, i dont know how to use this pattern, but i ll look at it now,
added the await you right, but it was just a test func didnt care to wait for that, not solving the error tho
okay, cool.
let me check.
thx
still bothered about this.
can you resend the exact error you get?
please do it with the this.repo.create - not sure that should be the problem as that is not how you have it in the other of your systems and it works fine.
just wannna see something.
what is the repo object
sorry, i am not getting how you should create it and where
dont code that much on ts, i am a golang main
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?
aha, maybe you teach me go.
sure, i'll share a file from my opensource project.
thx
check here: https://github.com/devodii/task-manager-app/blob/main/backend/src/user/user.service.ts#L27C1-L32C1
btw, i'm hacking on an alternative to linear app with Next js and Nest js π
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
not really, the scope too matters here.
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)
I think the problem is orm config.
have you verified that it has corresponding path to the other entities?
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
Okay.
Btw, why arent you using configservice to handle environment variables?
I can imagine that you like less abstractions π€£
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).
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
oh... okay.
thinking about the error though
can you share what is inside the entity? π
but evetho it might not be the best practice it should not be the problem, i guess as the other ones are working
sure
yep, just saying!
wow, that's too simple to throw an error, lol.
are you connecting to it from two services?
what you mean?
how many times are you doing TypeormModule.forFeature() on that entity?
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 { }
I'll get back to you in a bit.
Lemme go do some work π
oh sure
cool
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 π
advices for golang? or what? π
Hey man!
Yep, howz it better than JS?
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.
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
Okay, cool.
Plz let me know when you try this π
will try in a minute
okay
working on a golang thing now π , just a sec and i am done
Afternoon @restive siren
Did you try this? π€ π
Yoo man.
sorry man been on work trip
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
Okay, cool.
I'm gonna start learning go tonight.
Which YouTube video do you recommend?
Okay, please let me know ππΌ
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
no videos, go tour is the best to start : https://go.dev/tour/welcome/1
can I see the data source file?
Okay
i dont even know if i have a datasource file
the path he give in hte error in the data source do net exist
Can you check what the datsource file is?
That may be the problem
If that would be the problem why all the rest works
I honestly wish i could make a copy of the codebase π