Search code examples
database-migrationloopbackjsbuilt-inloopback4

Loopback 4 - Creating database tables for built-in models?


For LB3 there is specific documentation on how create database tables for built-in models, but for LB4 it seems missing, the closest thing I could find is this page in the migration guide, but it just explain the difference in models persistence handling and the advantages on having the repositories handling it instead of the models themselves.

Moreover, already having a mysql-based datasource, trying to create a repository to handle Users persistence result in error since it seems not to find the User built-in model.

enter image description here

How can I create table for built-int models, e.g. User, in LB4?

Update #1

Found this page in the documentation that tells how to list models to migrate and to use npm run migrate so I've tryied to put User in the list but got the foolowing error: so it seems to find/recognize the model but complains about it not having datasource settings in the model() decorator (I guess).

root@71827bda8df9:/home/node/app# npm run migrate

> [email protected] premigrate
> npm run build


> [email protected] build
> lb-tsc


> [email protected] migrate
> node ./dist/migrate

Migrating schemas (alter existing schema)
Cannot migrate database schema Error: Cannot migrate models not attached to this datasource: User
    at /home/node/app/node_modules/loopback-datasource-juggler/lib/datasource.js:1146:12
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

Solution

  • Long story short

    Cannot create tables for built-in models.

    Why?

    Found this SO question with a similar problem, and the accepted answer links to this docs page where built-in models are still marked as TBD almost 4 years later.

    So what now?

    I guess I have to create my own user model and integrate it with what LB4 offers me to handle users authentication.