- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 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 的同事,我们按如下方式解决了问题。
我们定义
有趣的是,没有为 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/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!