sourcefuse / loopback4-starter

Loopback 4 starter application. Multi-tenant architecture supported. Authentication, Authorization, Soft deletes, environment vars, Audit logs, included.
MIT License
158 stars 59 forks source link

user_id needs to be changed to userId #33

Closed kjellouli closed 4 years ago

kjellouli commented 4 years ago

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

samarpan-b commented 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 ?

kjellouli commented 4 years ago

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.

agnes512 commented 4 years ago

Hi @kjellouli ! Hope my comments in this PR would be helpful https://github.com/strongloop/loopback-next/pull/3772#discussion_r326426738

samarpan-b commented 4 years ago

@kjellouli can you share a sample where I can see the error happening ? For me it works actually.

nmabhinandan commented 4 years ago

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' })
samarpan-b commented 4 years ago

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.

samarpan-b commented 4 years ago

Should be fixed now.