i'm facing a issue when trying to connect my Nest.js API to my database. Works fine when i'm using a local database.
My database is deployed on Heroku server and i'm able to access it with pgadmin.
I'm using TypeORM with a .env file for configuration.
.env example:
NODE_TLS_REJECT_UNAUTHORIZED=0
TYPEORM_CONNECTION=postgres
TYPEORM_HOST=***
TYPEORM_USERNAME=***
TYPEORM_PASSWORD=***
TYPEORM_DATABASE=***
TYPEORM_PORT=5432
TYPEORM_DRIVER_EXTRA='{"ssl":true}'
TYPEORM_SYNCHRONIZE=false
TYPEORM_ENTITIES=dist/**/*.entity.js
TYPEORM_MIGRATIONS=dist/v1/migration/*.js
TYPEORM_MIGRATIONS_DIR=src/v1/migration
TYPEORM_MIGRATIONS_RUN=true
Running the npm run start:dev cmd return this error
[Nest] 14998 - 2020-04-01 17:02:42 [TypeOrmModule] Unable to connect to the database. Retrying (1)... +712ms
QueryFailedError: syntax error at or near "`"
at new QueryFailedError (/home/primeradiant/Documents/hitema/agorise/projet/easymove-api/node_modules/typeorm/error/QueryFailedError.js:11:28)
at Query.callback (/home/primeradiant/Documents/hitema/agorise/projet/easymove-api/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
at Query.handleError (/home/primeradiant/Documents/hitema/agorise/projet/easymove-api/node_modules/pg/lib/query.js:138:19)
at Connection.connectedErrorMessageHandler (/home/primeradiant/Documents/hitema/agorise/projet/easymove-api/node_modules/pg/lib/client.js:223:17)
at Connection.emit (events.js:210:5)
at TLSSocket.<anonymous> (/home/primeradiant/Documents/hitema/agorise/projet/easymove-api/node_modules/pg/lib/connection.js:120:12)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:308:12)
at readableAddChunk (_stream_readable.js:289:11)
at TLSSocket.Readable.push (_stream_readable.js:223:10)
I don't know if the error is due to wrong credentials or not. It's possible that I didn't understand a point. Thank's for the help
Edit: The issue is comming from the TYPEORM_MIGRATIONS_RUN set to true, if I set it to false the api build successfully
Or is comming from the query file that auto migration generate
import {MigrationInterface, QueryRunner} from "typeorm";
export class BaseHistory1584471283256 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("DROP INDEX `IDX_97672ac88f789774dd47f7c8be` ON `users`");
await queryRunner.query("CREATE TABLE `history` (`id` int NOT NULL AUTO_INCREMENT, `price` varchar(255) NOT NULL, `departure_station` varchar(255) NOT NULL, `created_at` datetime NOT NULL DEFAULT NOW(), `userId` varchar(255) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB");
await queryRunner.query("ALTER TABLE `users` DROP COLUMN `createdAt`");
await queryRunner.query("ALTER TABLE `users` DROP COLUMN `updatedAt`");
await queryRunner.query("ALTER TABLE `users` ADD `created_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)");
await queryRunner.query("ALTER TABLE `users` ADD `updated_at` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)");
await queryRunner.query("ALTER TABLE `users` ADD UNIQUE INDEX `IDX_97672ac88f789774dd47f7c8be` (`email`)");
await queryRunner.query("ALTER TABLE `history` ADD CONSTRAINT `FK_7d339708f0fa8446e3c4128dea9` FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON DELETE CASCADE");
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("ALTER TABLE `history` DROP FOREIGN KEY `FK_7d339708f0fa8446e3c4128dea9`");
await queryRunner.query("ALTER TABLE `users` DROP INDEX `IDX_97672ac88f789774dd47f7c8be`");
await queryRunner.query("ALTER TABLE `users` DROP COLUMN `updated_at`");
await queryRunner.query("ALTER TABLE `users` DROP COLUMN `created_at`");
await queryRunner.query("ALTER TABLE `users` ADD `updatedAt` datetime(6) NOT NULL DEFAULT 'CURRENT_TIMESTAMP(6)'");
await queryRunner.query("ALTER TABLE `users` ADD `createdAt` datetime(6) NOT NULL DEFAULT 'CURRENT_TIMESTAMP(6)'");
await queryRunner.query("DROP TABLE `history`");
await queryRunner.query("CREATE INDEX `IDX_97672ac88f789774dd47f7c8be` ON `users` (`email`)");
}
}
ormconfig.jsonor yourTypeormModule.forRoot/Async()(whichever you use) then I think it would be better