You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
@Entity()exportclassPost{
@ObjectIdColumn()_id: ObjectID;
@Column()title: string;}// try to delete a post by passing directly it's ObjectId (as the signature suggests should be possible)// delete(criteria: string|string[]|number|number[]|Date|Date[]|ObjectID|ObjectID[]|FindConditions<Entity>): Promise<DeleteResult>constpost: Post=getSomePost();repository.delete(post._id);
If you look at the actual query executed (in MongoQueryRunner) you will see deleteOne({}).
Notice the empty query! This will have a side effect of removing the first Post from the collection, whatever that is ...
If the Post entity has the id field renamed to postId the query will be deleteOne({ _id: Buffer}), which is still wrong, because the type should be ObjectId not Buffer. In this case the sideffect is less severe, it just doesn't delete anything
The workaround is to use repository.delete({_id: post._id}) which works correctly.
protectedconvertMixedCriteria(metadata: EntityMetadata,idMap: any): ObjectLiteral{if(idMapinstanceofObject){returnmetadata.columns.reduce((query,column)=>{constcolumnValue=column.getEntityValue(idMap);if(columnValue!==undefined)query[column.databasePath]=columnValue;returnquery;},{}asany);}// means idMap is just object idconstobjectIdInstance=PlatformTools.load("mongodb").ObjectID;return{"_id": (idMapinstanceofobjectIdInstance) ? idMap : newobjectIdInstance(idMap)};}
If idMap is an ObjectId it will obviously also be an instance of Object and it will enter wrongly in the first branch, never reaching the code at the bottom.
The text was updated successfully, but these errors were encountered:
danmana
added a commit
to danmana/typeorm
that referenced
this issue
Aug 12, 2020
Issue type:
[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue
Database system/driver:
[ ]
cordova
[x]
mongodb
[ ]
mssql
[ ]
mysql
/mariadb
[ ]
oracle
[ ]
postgres
[ ]
cockroachdb
[ ]
sqlite
[ ]
sqljs
[ ]
react-native
[ ]
expo
TypeORM version:
[x]
latest
[ ]
@next
[ ]
0.x.x
(or put your version here)Steps to reproduce or a small repository showing the problem:
If you look at the actual query executed (in MongoQueryRunner) you will see
deleteOne({})
.Notice the empty query! This will have a side effect of removing the first Post from the collection, whatever that is ...
If the Post entity has the id field renamed to
postId
the query will bedeleteOne({ _id: Buffer})
, which is still wrong, because the type should be ObjectId not Buffer. In this case the sideffect is less severe, it just doesn't delete anythingThe workaround is to use
repository.delete({_id: post._id})
which works correctly.The problem is in MongoEntityManager.convertMixedCriteria which tests for instanceof ObjectId in the wrong order:
If
idMap
is an ObjectId it will obviously also be an instance of Object and it will enter wrongly in the first branch, never reaching the code at the bottom.The text was updated successfully, but these errors were encountered: