typeorm-polymorphic icon indicating copy to clipboard operation
typeorm-polymorphic copied to clipboard

AbstractPolymorphicRepository.findPolymorphs uses wrong entityType

Open tkausl opened this issue 4 years ago • 2 comments

Getting this error following the Readme:

(node:117404) UnhandledPromiseRejectionWarning: TypeError: val.slice is not a function
    at escapeString (D:\dev\mmdb\node_modules\sqlstring\lib\SqlString.js:202:23)
    at Object.escape (D:\dev\mmdb\node_modules\sqlstring\lib\SqlString.js:56:21)
    at Object.format (D:\dev\mmdb\node_modules\sqlstring\lib\SqlString.js:100:19)
    at PoolConnection.Connection.format (D:\dev\mmdb\node_modules\mysql\lib\Connection.js:271:20)
    at PoolConnection.query (D:\dev\mmdb\node_modules\mysql\lib\Connection.js:189:22)
    at MysqlQueryRunner.<anonymous> (D:\dev\mmdb\src\driver\mysql\MysqlQueryRunner.ts:182:36)
    at step (D:\dev\mmdb\node_modules\tslib\tslib.js:141:27)
    at Object.next (D:\dev\mmdb\node_modules\tslib\tslib.js:122:57)
    at fulfilled (D:\dev\mmdb\node_modules\tslib\tslib.js:112:62)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)

Tried debugging, https://github.com/bashleigh/typeorm-polymorphic/blob/3f41c5c3f8d923aa2995e2889b5dc717fae682d4/src/polymorphic.repository.ts#L175

Shouldn't this be parent.constructor.name? According to the query log, entityType always ends up as null in the query, as the entity instance isn't resolved to its name, and its the wrong entity anyway, as it references the parent's name in the child's entityType column, not itself.

tkausl avatar Jan 24 '21 12:01 tkausl

Hi @tkausl I made a bit of a mistake with this. I wrote this package well over a year ago and started writing tests a week ago. I realised I hadn't used the parent method and this only works for children. So at the moment a child can be saved with one or more parents but a parent cannot be saved with one or more children. I tried a quick fix last week and this needs more thought. I've realised I need to do the following

  • Save parent object first
  • build each child with polymorphic relations
  • save all children

Which doesn't sound difficult! However there can be many definitions! Therefore I need to do this in an async method with both children and parent relations which is the difficult bit. When I get a spare day or two I'll give it a go! Really need a rethink of the structure

bashleigh avatar Jan 24 '21 13:01 bashleigh

Having the same issue (val.slice is not a function), but when just trying to find parent.

meliborn avatar Feb 25 '21 23:02 meliborn