Hypotheses
Multiple occurrences of a single entity named
Product
are present.The
Product
entity has three attributes namely 1)id
, 2)name
, and 3)active
.The current instances stored in the database are as follows:
[ { id:1, name:'A', active: 0 }, { id:2, name:'B', active: 1 }, { id:3, name:'C', active: 0 }, ]
Issue
The goal is to modify certain properties of multiple instances based on their IDs using a query similar to the one below:
Product.update(
[1,2] , // ids
{
name: 'example'
}
)
The desired outcome should be:
[
{
id:1,
name:'example',
active: 0
},
{
id:2,
name:'example',
active: 1
},
{
id:3,
name:'C',
active: 0
},
]
However, this query overrides the other properties, specifically the active
attribute, setting it to default. The result obtained is as follows:
[
{
id:1,
name:'example',
active: 0
},
{
id:2,
name:'example',
active: 0 // incorrect value!
},
{
id:3,
name:'C',
active: 0
},
]
Inquiries
- Is there a way to only update specified properties in the query (e.g.,
name
)? - How can this issue be resolved using the
save
method? (Considering that passingids
to thesave
method is not possible)