I created a migration for an entity in Mikro Orm. After that, i modified the entity and ran the migrations again. Now nestjs keeps giving me this error.
This was my entity.
@Entity()
export class Task extends BaseEntity<Task, 'id'> {
@PrimaryKey()
id: number;
@Property()
name: string;
@Property()
description: string;
}
I created initial migration with npx mikro-orm migration:create --initial
. After that i modified my entity to this:
@Entity()
export class Task extends BaseEntity<Task, 'id'> {
@PrimaryKey()
id: number;
@Property()
name: string;
@Property()
description: string;
@Enum(() => TaskStatus)
status: TaskStatus = TaskStatus.OPEN;
}
export enum TaskStatus {
OPEN = 'OPEN',
IN_PROGRESS = 'IN_PROGRESS',
DONE = 'DONE',
}
After that, I ran two commands npx mikro-orm migration:create
& npx mikro-orm migration:up
.
Now NestJs keeps giving me this error.
[Nest] 13528 - 02/06/2021, 6:59:14 pm [NestFactory] Starting Nest application...
[Nest] 13528 - 02/06/2021, 6:59:14 pm [InstanceLoader] MikroOrmModule dependencies initialized +43ms
[Nest] 13528 - 02/06/2021, 6:59:14 pm [InstanceLoader] ConfigHostModule dependencies initialized +1ms
[Nest] 13528 - 02/06/2021, 6:59:14 pm [InstanceLoader] AppModule dependencies initialized +1ms
[Nest] 13528 - 02/06/2021, 6:59:14 pm [InstanceLoader] ConfigModule dependencies initialized +0ms
[Nest] 13528 - 02/06/2021, 6:59:14 pm [ExceptionHandler] Duplicate entity names are not allowed: Task +409ms
MetadataError: Duplicate entity names are not allowed: Task
at Function.duplicateEntityDiscovered (C:\dev\nodejs\nestjs\sandbox\node_modules\@mikro-orm\core\errors.js:151:16) at MetadataValidator.validateDiscovered (C:\dev\nodejs\nestjs\sandbox\node_modules\@mikro-orm\core\metadata\MetadataValidator.js:40:42)
at MetadataDiscovery.findEntities (C:\dev\nodejs\nestjs\sandbox\node_modules\@mikro-orm\core\metadata\MetadataDiscovery.js:81:24)
at processTicksAndRejections (node:internal/process/task_queues:93:5)
at async MetadataDiscovery.discover (C:\dev\nodejs\nestjs\sandbox\node_modules\@mikro-orm\core\metadata\MetadataDiscovery.js:34:9)
at async Function.init (C:\dev\nodejs\nestjs\sandbox\node_modules\@mikro-orm\core\MikroORM.js:42:24)
at async Injector.instantiateClass (C:\dev\nodejs\nestjs\sandbox\node_modules\@nestjs\core\injector\injector.js:290:37)
at async callback (C:\dev\nodejs\nestjs\sandbox\node_modules\@nestjs\core\injector\injector.js:42:30)
at async Injector.resolveConstructorParams (C:\dev\nodejs\nestjs\sandbox\node_modules\@nestjs\core\injector\injector.js:114:24)
at async Injector.loadInstance (C:\dev\nodejs\nestjs\sandbox\node_modules\@nestjs\core\injector\injector.js:46:9)
UPDATE
After investigation, the code can be run perfectly. There maybe some problems on the node_modules
where some incomplete/incompatible packages exist.
The solution is to remove node_modules
and reinstall again.
The steps you do is the following:
If you modify the Task class without running migration command before, you will end up having 2 migration files with create task table command because mikro does not have any references of task table before any migration happened.
You can check the SQL in both migration files, it should be create table task xxx
.
Therefore, there is a way to solve it:
npx mikro-orm migration:up
to create task table on DB without enumnpx mikro-orm migration:create
.
Right now, mikro has references of the task table and know that you want to alter table so the SQL in migration will be
alter table task
instead ofcreate table
npx mikro-orm migration:up
to alter task table on DB with enum