Connection "default" was not found with TypeORM

59,171

Solution 1

You are trying to create a repository or manager without the connection being established.

Try doing this const shopkeeperRepository = getRepository(Shopkeeper); inside a function. it will work

Solution 2

the upvoted answer is not necessarily correct, if you not specify the connection name it will default to "default".

const manager = getConnectionManager().get('your_orm_name');
const repository = manager.getRepository<AModel>(Model);

Solution 3

We are using lerna and using code from library A in package B.

The problem was that both TypeOrm versions in each package differ.

Solution is to make sure that you have exactly the same version installed in each package.

To be on the safe side, delete your node_modules directory and reinstall everything again with yarn install or npm install

Check your yarn.lock for multiple entries of typeorm and make sure there is only one.

Solution 4

If anyone else has this problem in the future, check this out just in case:

I accidentally did "user.save()" instead of "userRepo.save(user)".

(And of course above initializing the connection like this:

const userRepo = getConnection(process.env.NODE_ENV).getRepository(User))

Solution 5

If anyone using Express Router with getRepository(), check the code below

const router = Router();

router.get("/", async function (req: Request, res: Response) {
  // here we will have logic to return all users
  const userRepository = getRepository(User);
  const users = await userRepository.find();
  res.json(users);
});

router.get("/:id", async function (req: Request, res: Response) {
  // here we will have logic to return user by id
  const userRepository = getRepository(User);
  const results = await userRepository.findOne(req.params.id);
  return res.send(results);
});

Just make sure to call getRepository() in every route just like Saras Arya said in the accepted answer.

Share:
59,171
ValentinV
Author by

ValentinV

Updated on July 09, 2022

Comments

  • ValentinV
    ValentinV almost 2 years

    I use TypeORM with NestJS and I am not able to save properly an entity.

    The connection creation works, postgres is running on 5432 port. Credentials are OK too.

    However when I need to save a resource with entity.save() I got :

    Connection "default" was not found.
    
    
    Error
        at new ConnectionNotFoundError (/.../ConnectionNotFoundError.ts:11:22)
    

    I checked the source file of TypeORM ConnectionManager (https://github.com/typeorm/typeorm/blob/master/src/connection/ConnectionManager.ts) but it seems that the first time TypeORM creates connection it attributes "default" name if we don't provide one, which is the case for me.

    I setup TypeORM with TypeOrmModule as

    TypeOrmModule.forRoot({
          type: config.db.type,
          host: config.db.host,
          port: config.db.port,
          username: config.db.user,
          password: config.db.password,
          database: config.db.database,
          entities: [
            __dirname + '/../../dtos/entities/*.entity.js',
          ]
        })
    

    Of course my constants are correct. Any ideas ?

  • nomadoda
    nomadoda over 5 years
    Not if you're using active record pattern: github.com/typeorm/typeorm/blob/master/docs/…
  • jayarjo
    jayarjo over 4 years
    Ut works fine when I launch it with ts-node and starts to fail after transpilation. What do you mean two can't live together? Can you elaborate?
  • kbhaskar
    kbhaskar over 3 years
    I have tried that connectionManager but i am getting the same error.
  • kbhaskar
    kbhaskar over 3 years
    Thanks, it works. But calling getRepository in each route is not a good way.
  • Vetterjack
    Vetterjack over 2 years
    If you got this error when using npm link, use npm install $(npm pack <path_to_linked_lib> | tail -1) instead. This ensures that only one matching typeorm version is installed in the target project (if the typeorm semver versions match).