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
Connection "default" was not found with TypeORM
提问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 name
in my db config.
就我而言,问题是因为我传入name
了我的数据库配置。
export const dbConfig = {
name: 'myDB',
...
}
await createConnection(dbConfig) // like this
As a result, the only connection server knows is myDB
not default
.
结果,唯一的连接服务器知道myDB
不是default
。
At the same time, in my service, repository was injected without name
which will fallback to default
. (Service will looking for default
connection 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 name
property in my db config.
作为修复,我删除name
了数据库配置中的属性。
Or you can pass myDB
as a parameter for InjectRepository
like @InjectRepository('myDB')
, either way works.
或者您可以myDB
作为参数传递InjectRepository
like @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 typeORM
entity did have an @Entity()
decorator as well as that it had extended BaseEntity
.
由于我的typeORM
实体确实有一个@Entity()
装饰器并且它已经扩展了BaseEntity
。
The two can't live together.
两个人不能住在一起。