loopback-next
loopback-next copied to clipboard
npm run migrate : cannot migrate models not attached to this datasource, while having model sequencing in migrate.ts
- Visited https://loopback.io/doc/en/lb4/todo-list-tutorial-sqldb.html - #link1 -Visited https://loopback.io/doc/en/lb4/MySQL-connector.html - #link2
Steps to reproduce
-
Intstall @loopback/authentication @loopback/authentication-jwt extensions
-
Suppose you are using Relational database, using the constraints as foreign keys, so you to sequence the model in migrate.ts as guided on #link1 above like
models:[...model, ...relatedmodel]**Note if you dont have key constraints and this model sequencing, it work fine. no error -
Then bind the authentication-jwt datasource in to your application.ts
this.datasource(YourDbDatasource, UserServiceBindings.DATASOURCE_NAME) -
when you run migrate. `
npm run clean npm run build npm run migrate `
Error Come in action
Cannot migrate database schema Error: Cannot migrate models not attached to this datasource:
@dhmlau : IMO its not relating to @Authentication only, It is related to binding of datasource, if we have some tag related to datasource binding then please add this to it.
Just encountered the same issue when adding the authentication to my existing project (db migration was working properly before).
- using PostgreSql connector
- to validate the data migration, I commented the line in application.ts, binding the db auth : this.datasource(YourDbDatasource...). Then all changes in DB where correctly done
In my case, it happened on a fresh migration with new database (due to technical OS issues, I deleted the db before installing the authentication lib).
@madaky How did you fix it?
@madaky @dhmlau I am facing the same issue, is there any workaround for this bug?
I have already faced the same issue, while using this model
@model({ settings:{mysql:{schema:'db_user',table:'tbl_user'}}, })
Then I resolved this error by editing the code as, @model({ name:'tbl_user' })
I've tried to use the jwt extension and published a blog recently on using postgresql as the database for User model. https://mobilediana.medium.com/using-postgresql-with-loopback4-jwt-authentication-extension-d956ac707746.
If you have a link to a repo that would reproduce this migration problem, please share here and we can take a look. Thanks.
Got the same issue since I added "name" in the model. Without it it was working but the table was created with Uppercase letter at first char.
@model({
name: 'queue',
settings: {
foreignKeys: {
fk_status_statusId: {
name: 'fk_status_statusId',
entity: 'Status',
entityKey: 'id',
foreignKey: 'statusId',
},
},
},
})
Edit: In opposition to @Arathy-sivan I solved it by remplacing name with : settings: {mysql: {table: 'queue'},}
@model({
settings: {
mysql: {table: 'queue'}, // <-- here
foreignKeys: {
fk_status_statusId: {
name: 'fk_status_statusId',
entity: 'Status',
entityKey: 'id',
foreignKey: 'statusId',
},
},
},
})
I am facing the same issue, is there any workaround for this issue?
I've tried to use the jwt extension and published a blog recently on using postgresql as the database for User model. https://mobilediana.medium.com/using-postgresql-with-loopback4-jwt-authentication-extension-d956ac707746.
If you have a link to a repo that would reproduce this migration problem, please share here and we can take a look. Thanks.
I created the tables using above article and added the following lines at the end of application.ts
// Bind datasources
this.dataSource(PostgresDataSource, 'postgres'); // the datasource of the models I created
this.dataSource(PostgresDataSource, 'jwtdb'); // the datasource for User auth, as stated in LB 4 documentation
Then it worked. Hope this helps.
Hi, I'm also with the same issue, I just have a PostgreSQL and kv-redis connectors working without problems, but when I installed a new MongoDB connector. The migrateSchema returns:
Cannot start the application. Error: Cannot migrate models not attached to this datasource: Instances States at /Users/osuarez/Documents/GitHub/forward-api/node_modules/loopback-datasource-juggler/lib/datasource.js:1146:12
at process._tickCallback (internal/process/next_tick.js:61:11)
My config for Mongo connector includes "disableMigration": true
I'm not sure how to work around this issue, any advice?
Hi, same issue here. MySQL / MongoDB connectors, can't migrate any of them
Don't know what changed, everything was working OK some months ago.
I don't have the binding line this.dataSource(MySqlDbDataSource, 'mySqlDb'); on application.ts, instead, I've injected it on every repository's constructor, like this:
`
constructor(
@inject('datasources.mySqlDb') dataSource: MySqlDbDataSource,
// Other stuff ) `
Some repositories use MySqlDataSource, while others use MongoDbDataSource
Any news/tips/comments? We're unable to migrate (ie: update DB structure)
I had the same problem. I found that what I was missing was the REPOSITORY that connects the Model with the Datasource.
lb4 repository
and then you can select the DataSource and the Models that you want to connect with it.
Hit this error too. Similar to @madaky I had the Todo example (extended with TodoList) running with MySQL with no problems. Then followed the JWT Authentication tutorial to try to add authentication.
Trying to migrate the database returns the error:
Migrating schemas (alter existing schema)
Cannot migrate database schema Error: Cannot migrate models not attached to this datasource: TodoList Todo User
at C:\...\todo-list\node_modules\loopback-datasource-juggler\lib\datasource.js:1146:12
at processTicksAndRejections (node:internal/process/task_queues:78:11)
Without DB migration the app will run, but trying POST /signup to the UserController returns a 500 statusCode and the terminal console logs error that User table does not exist:
WARNING: relational database doesn't support {strict: false} mode. {strict: true} mode will be set for model User instead.
Request POST /signup failed with status code 500. Error: ER_NO_SUCH_TABLE: Table 'loopback_todo.User' doesn't exist
Update: tried following the blog post mentioned above by @dhmlau using Option 2.
However, still hit the same error when trying to migrate:
Migrating schemas (alter existing schema)
Cannot migrate database schema Error: Cannot migrate models not attached to this datasource: TodoList Todo User UserCredentials
at C:\...\todo-list-Auth\node_modules\loopback-datasource-juggler\lib\datasource.js:1146:12
at processTicksAndRejections (node:internal/process/task_queues:78:11)
Update: was successful with blog post Option 1, which is manually adding the SQL tables to the existing database. This was painful, and not easy to maintain, but at least it works for now.
Using MySQL database , here are the SQL commands used to create the tables and the foreign key (note: change the database name from loopback_todo to whatever your database is named):
CREATE TABLE `loopback_todo`.`UserCredentials` ( `id` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL , `password` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL , `userId` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
CREATE TABLE `loopback_todo`.`User` ( `id` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL , `realm` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL , `username` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL , `email` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL , `emailVerified` TINYINT(1) NULL DEFAULT NULL , `verificationToken` VARCHAR(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL , PRIMARY KEY (`id`) USING BTREE, UNIQUE (`email`)) ENGINE = InnoDB;
ALTER TABLE `loopback_todo`.`UserCredentials` DROP INDEX `fk_userCredentials_userId`, ADD UNIQUE `fk_userCredentials_userId` (`userId`) USING BTREE;
I was getting a similar error when following along with the todo-list example and then tried to follow the jwt example and use mssql instead of in-memory db. Error output look like:
Cannot migrate database schema Error: Cannot migrate models not attached to this datasource: TodoList Todo TodoListImage
at /home/chaseo/repos/todo-list/node_modules/loopback-datasource-juggler/lib/datasource.js:1146:12
but then I updated the migrate.ts for the following

and this fixed my error and allowed me to migrate successfully.
I had the same issue. Make sure you don't have 2 datasources under datasources/. I had 2: memory and postgresql. By running the script with a few console.log (I know that's debugging level 0), I got to see that the migrate script runs fine for postgresql then errors out for the memory. It makes sense since all my repositories are bound to postgresql and not memory.
EDIT: Having N datasources is fine, the actual problem was that both my datasources had the same @inject tag!
diff --git a/src/datasources/memory.datasource.ts b/src/datasources/memory.datasource.ts
index 8703f89..474366f 100644
--- a/src/datasources/memory.datasource.ts
+++ b/src/datasources/memory.datasource.ts
@@ -13,7 +13,7 @@ export class MemoryDataSource extends juggler.DataSource {
static readonly defaultConfig = config;
constructor(
- @inject('datasources.config.db', { optional: true })
+ @inject('datasources.config.memory', { optional: true })
On this dump, you can see it runs first for DbDataSource.DataSource.autoupdate, works fine. THEN runs for MemoryDataSource.DataSource.autoupdate where all the models are invalid.
$ node -r dotenv/config ./dist/migrate
Migrating schemas (alter existing schema)
[
'Attendance',
'Event',
'RoomAccessDecision',
'RoomAccess',
'RoomAttendance',
'RoomGroup',
'Room',
'User',
'VenueSpace',
'Venue'
]
Error
at DbDataSource.DataSource.autoupdate (/Users/adrien/Documents/personel/eva/eva-rest/node_modules/loopback-datasource-juggler/lib/datasource.js:1135:11)
at EvaRestApplication.migrateSchema (/Users/adrien/Documents/personel/eva/eva-rest/node_modules/@loopback/repository/dist/mixins/repository.mixin.js:249:40)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async migrate (/Users/adrien/Documents/personel/eva/eva-rest/dist/migrate.js:10:5)
Attendance true
RoomAccessDecision true
RoomGroup true
User true
Venue true
Room true
RoomAccess true
RoomAttendance true
VenueSpace true
Event true
...
[]
Error
at MemoryDataSource.DataSource.autoupdate (/Users/adrien/Documents/personel/eva/eva-rest/node_modules/loopback-datasource-juggler/lib/datasource.js:1135:11)
at EvaRestApplication.migrateSchema (/Users/adrien/Documents/personel/eva/eva-rest/node_modules/@loopback/repository/dist/mixins/repository.mixin.js:249:40)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async migrate (/Users/adrien/Documents/personel/eva/eva-rest/dist/migrate.js:10:5)
Attendance false
RoomAccessDecision false
RoomGroup false
User false
Venue false
Room false
RoomAccess false
RoomAttendance false
VenueSpace false
Event false