Strongloop:Polymorphic HasAndBelongsTuMany与uuids的关系

时间:2015-07-22 14:47:00

标签: strongloop

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

让我解释一下:

我有两个型号:CartCollection和Cart。一个系列应该有不同类型的推车,包括推车本身。 Cart和CartCollection有uuids而不是简单的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在通过模型中忽略了我的属性定义。它仍然是一个数字,您可以在资源管理器中的模型架构中看到:

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

有人知道我做错了什么或者它是Strongloop中的错误吗?

祝你好运

的Niclas

1 个答案:

答案 0 :(得分:1)

我是Niclas的同事,我们解决了以下问题。

我们定义

  • "购物车通过CartCartCollectionLink"关系多态" as cart"
  • " CartCollection hasMany Cart通过CartCartCollectionLink" polymorphic" as cartCollection"
  • 这里有重要的部分:两个多态" CartCartCollectionLink属于[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"
      }
    }
  }
}

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"
      }
    }
  }
}