'Connection \"default\" was not found on named connection

I have multiple connections using typeorm getConnections as follows

await createConnections([
  {
    name: 'main',
    type: 'mysql',
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASS,
    database: process.env.DB,
    logging: true,
    entities: [Property],
  },
  {
    name: 'favourites',
    type: 'postgres',
    url: process.env.DATABASE_URL,
    logging: false,
    synchronize: true,
    migrations: [path.join(__dirname, './migrations/*')],
    entities: [User],
  },
]);

In my Property entity as defined in the 1st connection under the name main I have the following, stating the database the entity is to use.

import { ObjectType, Field } from 'type-graphql';
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from 'typeorm';

@ObjectType()
@Entity({ database: 'main', name: 'property' })
class Property extends BaseEntity {
  @Field()
  @PrimaryGeneratedColumn()
  id!: number;

  @Field(() => String)
  @Column({ name: 'account_number' })
  accountNumber!: string;
}

export default Property;

However, in a GraphQL resolver, when I try

await Property.findOne(1234)

I get the following error

ConnectionNotFoundError: Connection \"default\" was not found.
    at ConnectionNotFoundError.TypeORMError [as constructor] 

However, when I remove the name property in the first connection in createConnections and remove the database property in the Property Entity constructor, it works.

So, how do I get this to work on a named connection?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source