Connection \"default\" was not found.

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.22

Steps to reproduce or a small repository showing the problem:

I have my createConnection file as follows.

import { getConnectionOptions, createConnection, Connection } from "typeorm";

export let connection: Connection | undefined = undefined;

export const createTypeORMConnection = async () => {
  console.log(process.env.NODE_ENV!);
  const connectionOptions = await getConnectionOptions(process.env.NODE_ENV!);
  console.log(connectionOptions);
  connection = await createConnection(connectionOptions);
  return connection;
};

The console logs are printing fine.

And my Resolver I am using the connection object exported here and using the save method.

console.log("env", process.env.NODE_ENV, connection!.name);
const user = await connection!
  .getRepository<User>(User)
  .create({
    firstName,
    lastName,
    email,
    password: hashedPassword
  })
  .save();
console.log(`register - Create User: ${user} -> Done`);

but before hitting the register - create user console log in the save method itself I am getting Connection default was not found.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12

github_iconTop GitHub Comments

19reactions
thiagovasconcelloscommented, Aug 14, 2020

Guys, I was facing the same problem. Took me a while to realize that I was calling a repository outside the method:

import { getCustomRepository } from 'typeorm';
import CustomerRepository from '../repositories/CustomerRepository';
import { Customer } from '../entities/Customer';

interface IRequest {
  name: string;
  contactPhoneId: string;
  isValid: boolean;
}
const customerRepository = getCustomRepository(CustomerRepository); // this is WRONG!!!

class CreateCustomerService {
  public async execute({
    name,
    contactPhoneId,
    isValid,
  }: IRequest): Promise<Customer> {
    const customer = customerRepository.create({
      name,
      contactPhoneId,
      isValid,
    });
    await customerRepository.save(customer);
    return customer;
  }
}

export default CreateCustomerService;

This method was called inside the routes of express and tries to connect to the repository before the connection even exists.

7reactions
SamRoehrichcommented, Jul 1, 2020

Had this same issue. I fixed it by spreading in the connection option to createConnection and changing the name to default const connectionOptions = await getConnectionOptions(NODE_ENV) createConnection({ ...connectionOptions, name: "default" } as any)

Ts lint did not like the override but the code works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection "default" was not found with TypeORM
Solution is to make sure that you have exactly the same version installed in each package. To be on the safe side, delete...
Read more >
0.3.0: Connection "default" was not found for creating entities ...
Issue Description The datasource I'm using: export const dataSource = new DataSource({ type: "postgres", url: process.env.
Read more >
Connection "default" was not found with TypeORM-postgresql
I use TypeORM with NestJS and I am not able to save properly an entity. The connection creation works, postgres is running on...
Read more >
connectionnotfounderror: connection default'' was not found
ConnectionNotFoundError: Connection "default" was not found. due to the fact that my typeORM entity did have an @Entity() decorator as well as that...
Read more >
Does anyone know any real world projects that use typeorm ...
"RepositoryNotFoundError: No repository for "MyModelName" was found. Looks like this entity is not registered in current "default" connection?".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found