postgresql TypeORM 未找到连接“默认”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49794140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:42:18  来源:igfitidea点击:

Connection "default" was not found with TypeORM

node.jspostgresqltypeormnestjs

提问by ValentinV

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

我将 TypeORM 与 NestJS 一起使用,但无法正确保存实体。

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

连接创建有效,postgres 在 5432 端口上运行。凭证也OK。

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

但是,当我需要使用 entity.save() 保存资源时,我得到了:

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.

我检查了 TypeORM ConnectionManager 的源文件(https://github.com/typeorm/typeorm/blob/master/src/connection/ConnectionManager.ts)但似乎 TypeORM 第一次创建连接时它属性为“默认”名称,如果我们不提供,我就是这种情况。

I setup TypeORM with TypeOrmModule as

我将 TypeORM 与 TypeOrmModule 设置为

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 ?

当然,我的常数是正确的。有任何想法吗 ?

回答by Saras Arya

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

尝试在const shopkeeperRepository = getRepository(Shopkeeper);函数内部执行此操作。它会起作用

回答by Dragos Podaru

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);

回答by ???

For those of you looking for another answer, check this out.

对于那些正在寻找其他答案的人,请查看此内容。

In my case, the issue was because I was passing namein my db config.

就我而言,问题是因为我传入name了我的数据库配置。

export const dbConfig = {
    name: 'myDB',
    ...
}

await createConnection(dbConfig) // like this

As a result, the only connection server knows is myDBnot default.

结果,唯一的连接服务器知道myDB不是default

At the same time, in my service, repository was injected without namewhich will fallback to default. (Service will looking for defaultconnection as a result)

同时,在我的服务中,注入了存储库,没有name它会回退到default. (服务将因此寻找default连接)

@Service() // typedi
export class Service {
    constructor(
        // inject without name -> fallback to default
        @InjectRepository() private readonly repository
    ) {}
}

As a fix, I removed nameproperty in my db config.

作为修复,我删除name了数据库配置中的属性。

Or you can pass myDBas a parameter for InjectRepositorylike @InjectRepository('myDB'), either way works.

或者您可以myDB作为参数传递InjectRepositorylike @InjectRepository('myDB'),无论哪种方式都有效。

回答by amitben

Although Saras Arya has provided the correct answer, I have encountered the same error

虽然 Saras Arya 提供了正确答案,但我也遇到了同样的错误

ConnectionNotFoundError: Connection "default" was not found.

ConnectionNotFoundError:未找到连接“默认”。

due to the fact that my typeORMentity did have an @Entity()decorator as well as that it had extended BaseEntity.

由于我的typeORM实体确实有一个@Entity()装饰器并且它已经扩展了BaseEntity

The two can't live together.

两个人不能住在一起。