typeorm: 0.3.0: Connection "default" was not found for creating entities with dataSource

Issue Description

The datasource I’m using:

export const dataSource = new DataSource({
  type: "postgres",
  url: process.env.DATABASE_URL,
  migrations: [path.join(__dirname, "./migrations/*")],
  entities: [path.join(__dirname, "./entities/*")],
  subscribers: [path.join(__dirname, "./subscribers/*")],
  logging: !__prod__,
  migrationsRun: true,
  synchronize: true,
});

and for a Post entity if I do

dataSource.getRepository(Post).create({...someEntity}).save() (or getTreeRepository)

This will complain that there is no default connection, it does not happen for the other basic methods insert, update, delete etc, I think it will have this problem for other methods like remove though, which aren’t sql methods

Expected Behavior

The post should be created and saved properly along with its tree parent/children if it exists, like in 0.2.x versions

Actual Behavior

Connection "default" was not found

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don’t know how to start. I would need guidance.
  • ✅ No, I don’t have the time, but I can support (using donations) development. 5$
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 16 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I can confirm this issue still exists in 0.3.17

What is the accepted way of declaring a DataSource and also using BaseEntity (ActiveRecord pattern)?

I always get ConnectionNotFoundError: Connection "default" was not found.

Trying to migrate from typeorm 0.2.20 to 0.3.17 and can not figure out how to migrate the code base from ConnectionOptions to DataSource.

Anybody already been through the process?

I was having this problem when using

@InjectEntityManager()
private readonly entityManager: EntityManager

this.entityManager.getRepository(User)

So I switched to

@InjectDataSource()
public readonly dataSource: DataSource;

this.dataSource.getRepository(User)

And I created a module called DatabaseModule

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.getOrThrow('TYPEORM_HOST'),
        port: configService.getOrThrow('TYPEORM_PORT'),
        username: configService.getOrThrow('TYPEORM_USERNAME'),
        password: configService.getOrThrow('TYPEORM_PASSWORD'),
        database: configService.getOrThrow('TYPEORM_DATABASE'),
        logging: configService.getOrThrow('TYPEORM_LOGGING'),
        synchronize: false,
        autoLoadEntities: true
      }),
      inject: [ConfigService],
    }),
  ],
})
export class DatabaseModule {}

my app.module

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    AuthModule,
    MailerModule,
    ModelsModule,
    SharedModule,
    DatabaseModule,
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

This worked perfectly for me replacing the deprecated getRepository(), and apparently it works well inside entities too, this has been my solution so far

Screenshot from 2022-11-22 20-41-51

okay I think I know what is the problem. For now you need to set YourEntity.useConnection(dataSource) for each entity you have as a hotfix. I’ll push a PR for this issue.

I am using 0.3.10 still facing same issue.

okay I think I know what is the problem. For now you need to set YourEntity.useConnection(dataSource) for each entity you have as a hotfix. I’ll push a PR for this issue.

do you have entities extending BaseEntity ?