typeorm: CannotExecuteNotConnectedError when running migration on Postgres
Issue Description
When trying to run a migration on Postgres DB, the migrations fail to run with CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established. at DataSource.destroy (/home/assaf/Code/private/f3tw/src/data-source/DataSource.ts:284:19) at Object.handler (/home/assaf/Code/private/f3tw/src/commands/MigrationRunCommand.ts:73:46)
Expected Behavior
All migrations should run as expected.
Actual Behavior
No migrations are running, and an error is thrown:
> typeorm-ts-node-esm -d src/data-source.ts "migration:run"
query: SELECT * FROM current_schema()
typeorm-ts-node-esm migration:run
Runs all pending migrations.
Options:
-h, --help Show help [boolean]
-d, --dataSource Path to the file where your DataSource instance is defined.
[required]
-t, --transaction Indicates if transaction should be used or not for
migration run. Enabled by default. [default: "default"]
-v, --version Show version number [boolean]
CannotExecuteNotConnectedError: Cannot execute operation on "default" connection because connection is not yet established.
at DataSource.destroy (/home/assaf/Code/private/f3tw/src/data-source/DataSource.ts:284:19)
at Object.handler (/home/assaf/Code/private/f3tw/src/commands/MigrationRunCommand.ts:73:46)
Steps to Reproduce
- Create a migration
- try to run it with
npm run typeorm migration:run
my datasource:
import "reflect-metadata";
import { DataSource } from "typeorm";
export const TestAppDataSource = new DataSource({
type: "postgres",
host: "localhost",
port: 5432,
username: "postgres",
password: "test",
database: "postgres",
logging: process.env.NODE_ENV !== "production",
entities: ["src/entity/*.{js,ts}"],
migrations: ["src/migration/*.{ts, ts}"],
});
From looking at the code in src/commands/MigrationRunCommand.ts it seems like it does call
await dataSource.initialize() so I have no idea where that problem is coming from.
My Environment
~Might be related to the fact that I am using npm workspace and running inside one of the projects?~
It does work when using typeorm-ts-node-commonjs but not typeorm-ts-node-esm
| Dependency | Version |
|---|---|
| Operating System | Arch linux |
| Node.js version | 17.8.0 |
| Typescript version | 4.6.3 |
| TypeORM version | 0.3.5 |
Additional Context
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.
- ✖️ 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: 9
- Comments: 29 (2 by maintainers)
I found today that this error may be because of another completely unrelated error. I added a
console.loginnode_modules/typeorm/commands/MigrationRunCommand.jssuggested by lucasgama335, but instead I logged the error in the catch statement and found that I had accidentally used arequirein an ecmascript module.I didn’t found a solution @assapir but i could finally run the migrations.
Isn’t the best solution ever but at least we could go to sleep without this exception on mind lol 😆
After a couple of hours of investigation I also came to this conclusion: If you see the error below after writing some migrations:
and it even shows when running
typeorm migration:showthen you probably have problem with connection or your migration code! (EDIT: or, as it turns out, entity code, including incorrect column type).
Unfortunately, typeorm (or typeorm cli, or anything related) hides all compilation errors from migrations. The best way to learn what is wrong with your migration is to run:
This will show you actual errors in the code syntax and will help you solve them. In my case, it was just a typo in a variable name, but cli always reported
CannotExecuteNotConnectedError, so I spent way too long time digging into my connectivity params.I recommend everyone first attempt what lucasgama335 and Johann150 did and add a
console.log(err)in the catch block of thenode_modules/typeorm/commands/MigrationRunCommand.json line 65. For me, my issue was I was technically still connected in datagrip to an external DB through an ssh tunnel that was bound to the same local port as my typeorm connection and I was getting an auth error that wasn’t surfacing.I found the problem. import dotenv into your file that contains the dataSource. i also retrieve some environment variables and for some reason when dotenv does not matter the username comes as null.
My code: Configuration file: (@src/config/database.ts)
Configuration file: (@src/config/index.ts)
dataSource:
if it doesn’t work for you, go to your node_modules/typeorm/commands/MigrationRunCommand.js folder and console log the dataSource to verify the connection data is correct. I discovered my problem this way.
same issue with misskey migrations. edit: in the case of misskey, running
npm run clean-alland then installing the packages again made it possible to run the migrations as normal.I was able to fix my issue with
migration:showwith the advice from this reply.I had custom path aliases configured in my
tsconfig.jsonsuch that I could “pretty-import” modules within the project, i.e.import { User } from '@/user/user.entity. Turns out Typeorm doesn’t like this, and couldn’t resolve the module references while usingtypeorm-ts-node-commonjs. The error I was able to log fromnode_modules/typeorm/commands/MigrationShowCommand.jswasThis explains why I could use the Typeorm CLI fine from the built JS datasource, but using the TS datasource it was unable to resolve those references.
I had the same issue today and after some debugging like @acSpock told us I finally got an error message that helped me figuring out what seems to be the problem.
Including console.log(err) didn’t helped but commenting the dataSource.destroy() method out I got the espected result and an error:
I then found this article that solved my problems (Seems like I just had to define a password for connecting to an mysql database…) https://stackoverflow.com/questions/50093144/mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server
Hope that helps some of you.
Same issue
++ Same Issue