Closed kjellouli closed 4 years ago
This was done based on my observations as mentioned here - https://github.com/strongloop/loopback-next/issues/2639#issuecomment-495604244.
Are you suggesting otherwise ?
Yes, because that results in undefined foreign key values when I was debugging.
Let me know if that suits you and I'll open a PR.
Hi @kjellouli ! Hope my comments in this PR would be helpful https://github.com/strongloop/loopback-next/pull/3772#discussion_r326426738
@kjellouli can you share a sample where I can see the error happening ? For me it works actually.
It gives following error when auto migration is used without changing anything else
Cannot start the application. { Error: Invalid hasOne definition for users#credentials: target model UserCredentials is missing definition of foreign key usersId
at Object.resolveHasOneMetadata (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/repository/dist/relations/has-one/has-one.helpers.js:48:15)
at Object.createHasOneRepositoryFactory (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/repository/dist/relations/has-one/has-one-repository.factory.js:26:36)
at UserRepository.createHasOneRepositoryFactoryFor (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/repository/dist/repositories/legacy-juggler-bridge.js:210:28)
at new UserRepository (/home/nmabhinandan/dev/approval-management-system-backend/dist/repositories/user.repository.js:28:33)
at value_promise_1.transformValueOrPromise.args (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolver.js:46:16)
at Object.transformValueOrPromise (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/value-promise.js:227:16)
at Object.instantiateClass (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolver.js:41:34)
at _setValueGetter (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:407:46)
at Binding._getValue (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:295:20)
at resolution_session_1.ResolutionSession.runWithBinding.s (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:201:29)
at value_promise_1.tryWithFinally (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolution-session.js:69:53)
at Object.tryWithFinally (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/value-promise.js:157:18)
at Function.runWithBinding (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolution-session.js:69:32)
at Binding.getValue (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:199:67)
at ApprovalManagementSystemApplication.getValueOrPromise (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/context.js:654:36)
at ApprovalManagementSystemApplication.get (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/context.js:574:21)
code: 'INVALID_RELATION_DEFINITION',
relationName: 'credentials',
relationType: 'hasOne',
sourceModelName: 'users' }
Changing keyTo in user model to userId
will solve the problem
@hasOne(() => UserCredentials, { keyTo: 'userId' })
It gives following error when auto migration is used without changing anything else
Cannot start the application. { Error: Invalid hasOne definition for users#credentials: target model UserCredentials is missing definition of foreign key usersId at Object.resolveHasOneMetadata (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/repository/dist/relations/has-one/has-one.helpers.js:48:15) at Object.createHasOneRepositoryFactory (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/repository/dist/relations/has-one/has-one-repository.factory.js:26:36) at UserRepository.createHasOneRepositoryFactoryFor (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/repository/dist/repositories/legacy-juggler-bridge.js:210:28) at new UserRepository (/home/nmabhinandan/dev/approval-management-system-backend/dist/repositories/user.repository.js:28:33) at value_promise_1.transformValueOrPromise.args (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolver.js:46:16) at Object.transformValueOrPromise (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/value-promise.js:227:16) at Object.instantiateClass (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolver.js:41:34) at _setValueGetter (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:407:46) at Binding._getValue (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:295:20) at resolution_session_1.ResolutionSession.runWithBinding.s (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:201:29) at value_promise_1.tryWithFinally (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolution-session.js:69:53) at Object.tryWithFinally (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/value-promise.js:157:18) at Function.runWithBinding (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/resolution-session.js:69:32) at Binding.getValue (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/binding.js:199:67) at ApprovalManagementSystemApplication.getValueOrPromise (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/context.js:654:36) at ApprovalManagementSystemApplication.get (/home/nmabhinandan/dev/approval-management-system-backend/node_modules/@loopback/context/dist/context.js:574:21) code: 'INVALID_RELATION_DEFINITION', relationName: 'credentials', relationType: 'hasOne', sourceModelName: 'users' }
Changing keyTo in user model to
userId
will solve the problem@hasOne(() => UserCredentials, { keyTo: 'userId' })
Let me take a look.
Should be fixed now.
All keyFrom from this file needs to be changed to camelCase notation (e.g. user_id -> userId, and same for role_id and tenant_id) because assigning a specific role to a user fails and always default to role_id=1 which has all permissions.
https://github.com/sourcefuse/loopback4-starter/blob/e76d8d70f2484fb082204efa8431d3789436e8fd/src/models/user-tenant.model.ts#L19