-
-
Notifications
You must be signed in to change notification settings - Fork 4.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
Update maps returned instance to the wrong model #11364
Comments
How is it possible to have two tables with the same name? |
It's not that we have 2 tables with the same name. We have 2 models that are each bound to the same table. This is a very common scenario when you have a common table in the db with a discriminator column |
Hmmm, interesting, I never though about that, can you provide a more detailed example on how you use this kind of idea in a real scenario? (Give an example of discriminator, etc) Thanks |
Think about a read only application based around restaurants where all of the basic restaurant data is the same, but you are storing different types of restaurants all in the same table. You may classify the restaurants as FAST_FOOD, CASUAL, and FINE_DINING and you add a field called Now you could certainly create a A better way, IMO, would be to create 3 sequelize models and use a default scope where the default scope would always apply a filter on the By using the |
@a88zach I guess it might make sense... But have you considered just using multiple names scopes instead? const FastFoodRestaurant = Restaurant.scope("FAST_FOOD");
await FastFoodRestaurant.findAll();
// And same idea defining other scopes for the other types |
Interesting usecase, possible fix will be to remove sequelize/lib/query-interface.js Line 1040 in 398ae59
Model class set options.model whenever using queryInterface.bulkUpdate
|
@papb, that, indeed, would solve the issue for the example I gave above. However, that is not my real use case as it's a bit more complicated. We publish a shared npm package that exposes our data repositories and domain models that are derived from the sequelize models. This package is then consumed in our api and client projects so that the domain models can be used consistently throughout our code. However, we don't want to expose the sequelize methods to consumers as we only allow certain operations and consumers must go through our repository methods to interact with our database. Those repositories essentially wrap the sequelize methods we want to expose. For the domain models, we use a custom typing to strip away the sequelize stuff before we export so that we only expose the pojo. This is a Typescript project by the way.
We then export our raw domain models like so: This means that consumers can only build domain objects using the pojo and do not have access to all the sequelize goodies. This enforces that all consumers go through our data repositories Our models that are bound to the same database table are also made up of different attributes in some situations, so we cannot just use the scopes or the exposed models would contain properties that do not belong to specific domain models. However, regardless of the approach, I still feel like this is a bug with the update method that should be fixed. This specific bug only affects the update command and not others like create |
@a88zach Thanks for the detailed response. Looks complicated indeed. Well, I agree with you, if Can you try sushantdhiman's suggestion above and tell us if it works? |
@papb, the suggestion above is to make a change in the sequelize source code. I'm not set up right now, nor have the bandwidth to make/test changes to the source code. |
@a88zach I understand, if later you have the time for it let us know. If someone else is willing to check it as well, it would be great. |
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂 |
Issue Description
When multiple models are wired up to the same table. The model's static update method can incorrectly map the returned instance to the wrong model
What are you doing?
What do you expect to happen?
The updated instance should be an instance of Bar
What is actually happening?
The updated instance is an instance of Foo
Additional context
The root cause if this line:
https://github.com/sequelize/sequelize/blob/master/lib/query-interface.js#L1040
Which is called from the update method here:
https://github.com/sequelize/sequelize/blob/master/lib/model.js#L3219
The returned instances are being mapped to a model based on the underlying table name. The instance should be mapped to the same model used to call the update method
Environment
Issue Template Checklist
How does this problem relate to dialects?
Would you be willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: