Strongloop: Polymorphic HasAndBelongsToMany relation with uuids -
i try use polymorphic hasandbelongstomany relation while using uuids, too. problem is, can't teach strongloop use id's string type instead of number in necessary many-to-many-table. leads sql-errors while creating new relations.
let me explain example:
i have 2 models: cartcollection , cart. collection should have different kind of carts including cart itself. cart , cartcollection have uuids instead of simple ids. defining property in model-json works far. problem polymorphic many-to-many-relation between them. try use polymorphic hasandbelongstomany relation realize that. in table try override id-type well.
this json-code:
{ "name": "salecartcollection", "plural": "salecartcollections", "base": "persistedmodel", "idinjection": true, "options": { "validateupsert": true }, "properties": { "id": { "type": "string", "length": 36, "id": true } }, "validations": [], "relations": { "salecartspoly": { "type": "hasmany", "model":"salecart", "polymorphic" : { "as": "salecartspoly", "invert": true }, "through": "salecartcartcollectionlink" } }, "acls": [], "methods": [] } { "name": "salecart", "plural": "salecarts", "base": "persistedmodel", "idinjection": true, "options": { "validateupsert": true }, "properties": { "id": { "type": "string", "length": 36, "id": true } }, "validations": [], "relations": { "salecartcollections": { "type": "hasandbelongstomany", "model": "salecartcollection", "polymorphic": { "as":"salecartspoly", "foreignkey" : "salecartspolyid", "discriminator" : "salecartspolytype" }, "through": "salecartcartcollectionlink" } }, "acls": [], "methods": [] } { "name": "salecartcartcollectionlink", "base": "persistedmodel", "properties": { "salecartspolyid": { "type": "string", "length": 36 } }, "validations": [], "relations": { }, "acls": [], "methods": [] }
if try post new cartcollection existing cart, output:
loopback:connector:mysql sql: insert `salecartcartcollectionlink`(`salecartspolyid`,`salecartspolytype`,`salecartcollectionid`) values(?,?,?), params: [null,"salecart","bad7a6fc-1798-49c5-a0cb-fa59eba5b3a4"] +8ms loopback:connector:mysql error: {"code":"er_bad_field_error","errno":1054,"sqlstate":"42s22","index":0} +11ms
i found out happening, because strongloop ignores property-definition in through-model. still number can see here in model-schema in explorer:
[ { "salecartspolyid": 0, "id": 0, "salecartspolytype": "", "salecartcollectionid": "" } ]
does has idea if i'm doing wrong or bug in strongloop?
best regards
niclas
i co-worker of niclas , solved our problem follows.
we define
- the "cart hasmany cartcollection through cartcartcollectionlink" relation polymorphic "as cart"
- the "cartcollection hasmany cart through cartcartcollectionlink" polymorphic "as cartcollection"
- and here comes important part: 2 polymorphic "cartcartcollectionlink belongsto [cart | cartcollection]" relations polymorphic " with polymorphic.idtype: string!
the interesting part is, idtype property not set correctly hasandbelongstomany relations.
someone might want take @ relation-definition.js:1566:
if (params.polymorphic) { var polymorphic = polymorphicparams(params.polymorphic); options.polymorphic = polymorphic; // pass through var accessor = params.through.prototype[polymorphic.as]; if (typeof accessor !== 'function') { // declare once // use name of polymorphic rel, not modelto // *** might want set idtype here: *** params.through.belongsto(polymorphic.as, { polymorphic: true }); } }
cartcartcollectionlink.json
{ "name": "salecartcartcollectionlink", "relations": { "salecarts": { "type": "belongsto", "model": "salecart", "foreignkey": "salecartid", "polymorphic": { "as": "salecart", "idtype": "string" } }, "salecartcollections": { "type": "belongsto", "model": "salecartcollection", "foreignkey": "salecartcollectionid", "polymorphic": { "as": "salecartcollection", "idtype": "string" } } } }
cart.json
{ "name": "salecart", "properties": { "id": { "type": "string", "length": 36, "id": true } }, "relations": { "salecartcollections": { "type": "hasmany", "model": "salecartcollection", "foreignkey": "salecartid", "through": "salecartcartcollectionlink", "polymorphic": { "as": "salecart" } } } }
cartcollection.json
{ "name": "salecartcollection", "properties": { "id": { "type": "string", "length": 36, "id": true } }, "relations": { "salecarts": { "type": "hasmany", "model": "salecart", "foreignkey": "salecartcollectionid", "through": "salecartcartcollectionlink", "polymorphic": { "as": "salecartcollection" } } } }
Comments
Post a Comment