Have created a datasource and all my db operations are working fine. But I am unable to run migrate script. I am getting the error as Cannot migrate database schema Error: The connector has not been initialized.
I attach my config.json for the datasource below.
{
"name": "postgres",
"connector": "postgresql",
"url": "postgres://postgres:postgres@localhost:5433/doc_patient_link",
"host": "localhost",
"port": 5433,
"user": "postgres",
"password": "postgres",
"database": "doc_patient_link",
"debug":true
}
Hi @praneshvrp1396, as per-bug report template, could you please provide a reproduction sandbox on GitHub so that we can better spot the issue?
Discover a model from a created Datasource.
Edit the existing migrate.ts as:
import { SampleDataSource } from './datasources'
export async function migrate(args: string[]) {
const ds = new SampleDataSource();
await ds.autoupdate('Coupon')
process.exit(0);
}
Run npm run migrate
D:\FoxSense\pranesh-ba\birth-assist-server>npm run migrate
> [email protected] migrate D:\FoxSense\pranesh-ba\birth-assist-server
> node ./dist/migrate
Cannot migrate database schema Error: Cannot migrate models not attached to this
datasource: Coupon
at D:\FoxSense\pranesh-ba\birth-assist-server\node_modules\loopback-datasour
ce-juggler\lib\datasource.js:1146:12
at processTicksAndRejections (internal/process/task_queues.js:76:11)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] migrate: `node ./dist/migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\siva1\AppData\Roaming\npm-cache\_logs\2020-02-20T12_54_45_
112Z-debug.log
Schema Migration sucessfull
@praneshvrp1396 Thanks for the extra info;
Taken from the Todo example, migrate.ts should look similar to this (using Application instead of juggler.Datasource directly):
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
// Node module: @loopback/example-todo
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {TodoListApplication} from './application';
export async function migrate(args: string[]) {
const existingSchema = args.includes('--rebuild') ? 'drop' : 'alter';
console.log('Migrating schemas (%s existing schema)', existingSchema);
const app = new TodoListApplication();
await app.boot();
await app.migrateSchema({existingSchema});
// Connectors usually keep a pool of opened connections,
// this keeps the process running even after all work is done.
// We need to exit explicitly.
process.exit(0);
}
migrate(process.argv).catch(err => {
console.error('Cannot migrate database schema', err);
process.exit(1);
});
Thanks for the response. I replicated the same and I am getting the below error when I run npm run migrate
> [email protected] migrate D:\FoxSense\pranesh-ba\birth-assist-server
> node ./dist/migrate
Cannot migrate database schema error: syntax error at or near "null"
at Connection.parseE (D:\FoxSense\pranesh-ba\birth-assist-server\node_module
s\pg\lib\connection.js:614:13)
at Connection.parseMessage (D:\FoxSense\pranesh-ba\birth-assist-server\node_
modules\pg\lib\connection.js:413:19)
at Socket.<anonymous> (D:\FoxSense\pranesh-ba\birth-assist-server\node_modul
es\pg\lib\connection.js:129:22)
at Socket.emit (events.js:223:5)
at addChunk (_stream_readable.js:309:12)
at readableAddChunk (_stream_readable.js:290:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead (internal/stream_base_commons.js:181:23) {
name: 'error',
length: 93,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '81',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1150',
routine: 'scanner_yyerror'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] migrate: `node ./dist/migrate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] migrate script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\siva1\AppData\Roaming\npm-cache\_logs\2020-02-20T15_15_14_
618Z-debug.log
This might be related: https://github.com/strongloop/loopback-connector-postgresql/issues/34
Got this fixed.
I tried migrating from a existing db which I created via pgadminwhere I did not fill the length property of the character varying type. This lead to all confusions.