Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in your foreign key creations instructions page. #732

Open
awongCM opened this issue Dec 22, 2024 · 0 comments
Open

Error in your foreign key creations instructions page. #732

awongCM opened this issue Dec 22, 2024 · 0 comments
Labels

Comments

@awongCM
Copy link

awongCM commented Dec 22, 2024

https://loopback.io/doc/en/lb4/todo-list-tutorial-sqldb.html#specify-the-foreign-key-constraints-in-todo-model
In your example below.

@model({
  settings: {
    foreignKeys: {
      fk_todo_todoListId: {
        name: 'fk_todo_todoListId',
        entity: 'TodoList',
        entityKey: 'id',
        foreignKey: 'todoListId',
      },
    },
  },
})
export class Todo extends Entity {
  //etc.
}

When following the setup above to run database migration for my local postgresdb as part of todo example tutorial, I encountered the following error:

Cannot migrate database schema error: column "todoListId" referenced in foreign key constraint does not exist
    at Parser.parseErrorMessage (/Users/andywongcheeming/Projects/poc/todo-list/node_modules/pg-protocol/dist/parser.js:283:98)

I was left puzzled why the instructions did not work as expected. I dig up online and my personal ChatGPT to understand if Postgres DB has case-sensitive when comes to table and field naming conventions. It turns out that it does.

When I go back and check my local postgres todo definitions via psql cli.

todo_db=# \d todo
                  Table "public.todo"
   Column   |  Type   | Collation | Nullable | Default
------------+---------+-----------+----------+---------
 id         | integer |           | not null |
 title      | text    |           | not null |
 desc       | text    |           |          |
 iscomplete | boolean |           |          |
 todolistid | integer |           |          |

It turns our the foreign key todolistid comes out as lower case by default when running Loopback db migration tool. The sample @model code above supplied as {foreignKey: 'todoListId'}.

I corrected it to use lower-case, dropped the database and started the migration all over again. The migration works this time.

Thus, I must ask - shouldn't the instructions be updated to use lowercase since the plugin creates tables and field names in lowercase by default?

@awongCM awongCM added the bug label Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant