-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Problems extending User model with loopback v3.3.0 #3215
Comments
The offending code is introduced by 9fe084f. Does your AccessToken model has a relation to User so that 9fe084f#diff-5d81af95449574544e7317f3eda2badeR693 won't have |
@raymondfeng thank you for the prompt response! I've updated the original ticket to include a few more details on steps to reproduce. To answer your question, with my example I've not defined any custom relationships with the builtin |
In your case, built-in |
I guess this opens the flood gates to architecture/design discussions related to extending the In the mean time I'll create a sample repo for this ticket which simulates the issue and also provides some options to resolve it. |
I've created a sample repo to explore the issue, please see the branches below. A rather simple user setup boot script has been added to trigger the issue. If you run
Worth noting, I have a project which originally alerted me to this issue. The project has a user model which extends |
cc @bajtos |
cc @ebarault who is the author of 9fe084f which introduced this regression. @greaterweb thank you for such detailed bug report! I think we should:
Thoughts? |
@bajtos with regards to point 4, I can certainly help provide some additional context with more specific examples for tests that once worked which now fail. I'm traveling this week for work so it may be a little delayed but I will try to update the sample repo with some of the test scenarios. A quick look though at the one project I tried the solution from
Without looking too closely at this the problem is likely related to the fact I have a custom model |
I know there are pretty good docs for managing users but I think these need to be extended to provide specific guidance on scenarios when you extend the In my use case, I extend the base model for a variety of reasons, here are some:
Ideally I think most devs in my situation are looking for any custom model extending the base |
+1 @greaterweb would you like to contribute this improvement to our documentation? I can help you to fill missing pieces once you have something to start with. See http://loopback.io/doc/en/contrib/doc-contrib.html to get started.
All your reasons make perfect sense to me.
+1 There are quite many things that models don't inherit right now, see these related issues:
Many of these changes would break backwards compatibility :( I think our best option for the short-term is to modify our project template in loopback-workspace to come with a subclassed |
@bajtos I wouldn't object to assisting with the documentation but to be perfectly honest, I still think I need a little clarification as to what the actual best practices are for extending the For the Loopback project I cite in above comments with the failing tests, all now seem to passing once again when using the As time permits I'll do some exploration on this and keep you posted. |
By default, the built-in
The first relation is actively used inside methods like So when setting up a custom User model (the model name should not matter at all - both
Alternatively, the app can create a custom access-token model too, let's call it
(I am typing this from my head, right now I don't have bandwidth to write a sample app to verify. There may be mistakes, sorry for that!) In #2971, @ebarault added support for polymorphic "belongsTo" relation where a single AccessToken model belongs to one of multiple User models. The setup instructions would be slightly different in that case. |
While implementing the warning message, I learned few more interesting facts: Model relations are inherited. If the application has custom The problem starts when the app is extending both |
Based on my comment above, the first set of instructions should be corrected to the following: So when setting up a custom User model while using the built-in AccessToken model, one needs to re-configure AccessToken's "belongsTo" relation to target the new User model. This can be configured in {
"AccessToken": {
"dataSource": "db",
"public": false,
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "userId"
}
}
}
} |
Thank you, @bajtos, very helpful information! As mentioned in the misconfigured access token PR something seems to be breaking the rules a bit in the custom implementation for my personal project. I might try to prune out some of the business logic and publish a version of the loopback backend for review. I'll investigate a bit further and report back. |
Worked for me as well. I had an error when updating the extended usermodels email.
|
I'll defer these two tasks for later. @greaterweb if you manage to trim down your app to something that can be shared, then please open a new issue. |
I have the similar situation with extend the user model, where can I upload this ? |
Thanks, @bajtos |
Description/Steps to reproduce
lb
)server/model-config.json
find the builtin model reference:and replace with the custom model reference:
the
user
name for the custom model as it is used as seen in other examples, I will retest with a custom model name as wellUser
as thebase
value (for example see loopback-example-access-control), an overly simplified example might look like:/server/boot/setup.js
) with the followingThe following will produce an error such as:
Expected result
The user should save without any errors.
Additional information
This example is not an issue for v2.x or v3.x up to 3.2.1, this only surfaced with v3.3.0.
Based on the outcome of issue 103 for loopback-example-access-control this may not actually be an issue at all and just needs some updates to the docs?
For v3.3.0 the bug is not present when you have a custom model with
User
as base and retain the builtinUser
model reference inmodel-config.json
. If it became a requirement to always haveUser
model in situations like this the documentation should be updated accordingly. It should also then be discouraged for using builtin models as the base for custom models with the same name (eg. User builtin vs user custom).The text was updated successfully, but these errors were encountered: