gpt4 book ai didi

Strongloop:与 uuid 的多态 HasAndBelongsToMany 关系

转载 作者:行者123 更新时间:2023-12-02 03:23:37 25 4
gpt4 key购买 nike

我在使用 uuid 时也尝试使用多态的 HasAndBelongsToMany 关系。我的问题是,我不能教 Strongloop 在必要的多对多表中将 id 与字符串一起用作类型而不是数字。这会在创建新关系时导致 SQL 错误。

让我用一个例子来解释:

我有两个模型:CartCollection 和 Cart。一个集合应该有不同种类的购物车,包括购物车本身。 Cart 和 CartCollection 有 uuid 而不是简单的 id。到目前为止,将其定义为 model-json 中的一个属性是可行的。问题是它们之间的多态多对多关系。我尝试使用多态的 HasAndBelongsToMany 关系来实现这一点。在此表中,我也尝试覆盖 id-type。

这是我的 JSON 代码:

{
"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": []
}

如果我现在尝试将新的 CartCollection 发布到现有购物车,我会得到以下输出:

loopback:connector:mysql SQL: INSERT INTO `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

我发现这是在发生,因为 Strongloop 忽略了我在 Through-model 中的属性定义。它仍然是一个数字,您可以在资源管理器的模型架构中看到:

[
{
"saleCartsPolyId": 0,
"id": 0,
"saleCartsPolyType": "",
"saleCartCollectionId": ""
}
]

有人知道我做错了什么还是 Strongloop 中的错误?

最好的问候

尼克拉斯

最佳答案

我是 Niclas 的同事,我们按如下方式解决了问题。

我们定义

  • “Cart hasMany CartCollection through CartCartCollectionLink”关系多态“as cart”
  • “CartCollection hasMany Cart through CartCartCollectionLink”多态“作为 cartCollection”
  • 重要的部分来了:两个多态“CartCartCollectionLink belongsTo [Cart | CartCollection]”关系多态“with polymorphic.idType: string!

有趣的是,没有为 HasAndBelongsToMany 关系正确设置 idType 属性。

有人可能想看一下 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 the name of the polymorphic rel, not modelTo

// *** you might want to 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"
}
}
}
}

购物车.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"
}
}
}
}

关于Strongloop:与 uuid 的多态 HasAndBelongsToMany 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com