【问题标题】:Strongloop: Polymorphic HasAndBelongsToMany relation with uuidsStrongloop:与 uuid 的多态 HasAndBelongsToMany 关系
【发布时间】:2015-07-22 14:47:00
【问题描述】:

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

让我用一个例子来解释:

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

这是我的 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 中的错误?

最好的问候

尼古拉斯

【问题讨论】:

    标签: strongloop


    【解决方案1】:

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

    我们定义

    • “Cart hasMany CartCollection through CartCartCollectionLink”关系多态“as cart”
    • “CartCollection hasMany Cart through CartCartCollectionLink”多态“as 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"
          }
        }
      }
    }
    

    【讨论】:

    • 感谢您的回答,实际上基于此:link 它应该与 hasAndBelongsToMany 类型一起使用。确实是我为每一侧的模型配置了这个,也许它应该只配置在 2 个多对多模型中的单个上。无论如何,您基于 js 文件分析的建议解决了我的问题。谢谢。
    猜你喜欢
    • 2015-09-26
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 2021-05-08
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多