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
- fixes #8762; #8759; #8758; #8757; — committed to typeorm/typeorm by pleerock 2 years ago
- fix: bugfixes introduced in 0.3.0 (#8764) * fixes #8762; #8759; #8758; #8757; — committed to typeorm/typeorm by pleerock 2 years ago
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
So I switched to
And I created a module called DatabaseModule
my app.module
This worked perfectly for me replacing the deprecated getRepository(), and apparently it works well inside entities too, this has been my solution so far
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
?