loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Error: Target model primary key does not exist.

Open skokaina opened this issue 7 years ago • 4 comments

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

  1. 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)
);
  1. 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

  1. create a project with the application generator
  2. create a postgres database and create the tables
  3. create the datasource to bind to the postgres db
  4. run lb4 discover to generate the TUser and TProfilePicture models --> OK
  5. run lb4 repository to generate the repositories for these two models --> OK
  6. 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

  1. node -e 'console.log(process.platform, process.arch, process.versions.node)' darwin x64 10.16.0

  2. 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]

skokaina avatar Sep 11 '19 08:09 skokaina

Transferring to loopback-next repo.

dhmlau avatar Sep 11 '19 17:09 dhmlau

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.

basavarajvs avatar Sep 16 '19 15:09 basavarajvs

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

odykyi avatar Mar 19 '20 14:03 odykyi

Closing due to inactivity. Feel free to open a new issue if the problem persists.

achrinza avatar Apr 14 '20 10:04 achrinza