Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
Validation Error: Using global entity manager instance methods for context specific actions is disallowed

Using TypeORM and getting this error:


ValidationError: Using global EntityManager instance methods for context specific actions is disallowed.
If you need to work with the global instance's identity map, use `allowGlobalContext` configuration option or `fork()` instead

The code that it corresponds to is below:


import { MikroORM } from "@mikro-orm/core";
import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import mikroConfig from "./mikro-orm.config";

const main = async () => {
  const orm = await MikroORM.init(mikroConfig);
  const post = orm.em.create(Post, {
    title: "my first post",
  });
  await orm.em.persistAndFlush(post);
  await orm.em.nativeInsert(Post, { title: "my first post 2" });
};

main().catch((error) => {
  console.error(error);
});

I am unsure where I need to use the .fork() method

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I faced a similar issue today when I upgraded the mikrorm setup from v4 to v5. After doing some RnD, I found the following changes helped me solve the mentioned error.

  1. In the config object which is passed to the MikroORM.init call, pass the following property

allowGlobalContext: true

  1. Don't directly use em to create database entry. Instead use the following code
const post = orm.em.fork({}).create(Post, {
    title: "my first post",
  });

The above changes should help you fix the error.

I am also very new to MikroORM. so, I am not sure why this error appears. But my uneducated guess is, they are restricting access to any changes to the global EntityManager em instance.

over 4 years ago · Santiago Trujillo Relatório

0

After doing some digging I found this solution:

  1. yarn install dotenv

  2. create a .env file in the root of the project

  3. In your .env file paste the following:

MIKRO_ORM_ALLOW_GLOBAL_CONTEXT = true

Problem solved!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda