Error: Target model primary key does not exist.
Description/Steps to reproduce
We are using loopback 4 with a postgres database.
The relation generator gets aborted with following error:
Generation is aborted: Error: Target model primary key does not exist.
We couldn't manage a way to have more debug information, and so far, the message isn't self explanatory.
#DB Tables
- User Table
CREATE TABLE IF NOT EXISTS public.t_user(
id UUID PRIMARY KEY DEFAULT uuid_generate_v1mc(),
id_picture UUID,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(150) NOT NULL,
tel VARCHAR(50),
mail VARCHAR(250),
birth_date DATE
CONSTRAINT fk_id_picture FOREIGN KEY (id_picture) REFERENCES t_profile_picture (id)
);
- Profile Table
CREATE TABLE IF NOT EXISTS public.t_profile_picture(
id UUID PRIMARY KEY DEFAULT uuid_generate_v1mc(),
id_user UUID NOT NULL,
uri VARCHAR NOT NULL,
name VARCHAR(250) NOT NULL,
CONSTRAINT fk_id_user FOREIGN KEY (id_user) REFERENCES t_user (id)
);
#Steps to reproduce
- create a project with the application generator
- create a postgres database and create the tables
- create the datasource to bind to the postgres db
- run
lb4 discoverto generate the TUser and TProfilePicture models --> OK - run
lb4 repositoryto generate the repositories for these two models --> OK - run
lb4 relation, select relationType:belongsTo, source model:TUser, target model:TProfilePicture--> KO
=> Error:
Generation is aborted: Error: Target model primary key does not exist.
Expected result
We expect the models and repository files to be update along with a dedicated controller as per the documentation output section at: https://loopback.io/doc/en/lb4/Relation-generator.html
Additional information
-
node -e 'console.log(process.platform, process.arch, process.versions.node)'
darwin x64 10.16.0 -
npm ls --prod --depth 0 | grep loopback
├── @loopback/[email protected] ├── @loopback/[email protected] ├── @loopback/[email protected] ├── @loopback/[email protected] ├── @loopback/[email protected] ├── @loopback/[email protected] ├── @loopback/[email protected] ├── @loopback/[email protected] └── [email protected]
Transferring to loopback-next repo.
I faced the same issue.. as a workaround, I changed the "id: 1" from generated model code to "id: true" and then things starting moving forward.
this works for me
@model({
settings: {
foreignKeys: {
fk_todo_todoListId: {
name: 'fk_todo_todoListId',
entity: 'TodoList',
entityKey: 'id',
foreignKey: 'todolistid',
},
},
},
})
export class Todo extends Entity {
//etc.
}
https://loopback.io/doc/en/lb4/todo-list-tutorial-sqldb.html#specify-the-foreign-key-constraints-in-todo-model
Closing due to inactivity. Feel free to open a new issue if the problem persists.