繁体   English   中英

在 sequelize 中使用另一个表连接两个表

[英]Join two tables using another table in sequelize

我有三个 MySQL 表: appapp_metadataecosystem

app表结构为:

+---------------+------------+----+---+-------+--------------+
|Field          |Type        |Null|Key|Default|Extra         |
+---------------+------------+----+---+-------+--------------+
|id             |bigint(20)  |NO  |PRI|NULL   |auto_increment|
|name           |varchar(255)|NO  |   |NULL   |              |
+---------------+------------+----+---+-------+--------------+

app_metadata 表结构为:

+---------------+------------+----+---+-------+--------------+
|Field          |Type        |Null|Key|Default|Extra         |
+---------------+------------+----+---+-------+--------------+
|id             |bigint(20)  |NO  |PRI|NULL   |auto_increment|
|app_id         |bigint(20)  |NO  |MUL|NULL   |              |
|tag_name       |varchar(255)|NO  |   |NULL   |              |
|tag_value      |text        |NO  |   |NULL   |              |
+---------------+------------+----+---+-------+--------------+

生态系统表结构为:

+---------------+------------+----+---+-------+--------------+
|Field          |Type        |Null|Key|Default|Extra         |
+---------------+------------+----+---+-------+--------------+
|id             |bigint(20)  |NO  |PRI|NULL   |auto_increment|
|name           |varchar(255)|NO  |   |NULL   |              |
+---------------+------------+----+---+-------+--------------+

我想使用 app_metadata 表中的tag_value column和 tag_name tag_name column (仅当其值为生态系统app_metadata table时)将app tableecosystem table连接起来。

这是我试图实现这个用例的代码,但它不会产生预期的结果。 实际结果:

{
  "apps": [
    {
      "id": 1,
      "name": "EXAMPLE",
      "appMetadata": [
        {
          "id": 1,
          "tagName": "ecosystem_id",
          "tagValue": "1",
          "appId": 1,
          "ecosystem": {
            "id": 1,
            "name": "default"
          }
        },
        {
          "id": 470,
          "tagName": "vcalmic",
          "tagValue": "2",
          "appId": 1,
          "ecosystem": {
            "id": 2,
            "name": "dc/ed1"
          }
        }
      ]
    }
  ]
}

预期结果:

{
  "apps": [
    {
      "id": 1,
      "name": "EXAMPLE",
      "appMetadata": [
        {
          "id": 1,
          "tagName": "ecosystem_id",
          "tagValue": "1",
          "appId": 1,
          "ecosystem": {
            "id": 1,
            "name": "default"
          }
        },
        {
          "id": 470,
          "tagName": "vcalmic",
          "tagValue": "2",
          "appId": 1,
          "ecosystem": null
        }
      ]
    }
  ]
}

或者这个

{
  "apps": [
    {
      "id": 1,
      "name": "EXAMPLE",
      "ecosystem": {
         "id": 1,
         "name": "default"
       },
      "appMetadata": [
        {
          "id": 1,
          "tagName": "ecosystem_id",
          "tagValue": "1",
          "appId": 1
        },
        {
          "id": 470,
          "tagName": "vcalmic",
          "tagValue": "2",
          "appId": 1
        }
      ]
    }
  ]
}
Models.ecosystem.hasOne(Models.appMetadata, { foreignKey: 'tagValue' });
Models.appMetadata.belongsTo(Models.ecosystem, { foreignKey: 'tagValue' });

const options = {
  where: { accountId },
  include: [
    {
      model: Models.appMetadata,
      include: [{ model: Models.ecosystem }],
    }
  ]
};
const apps = await Models.app.findAll(options);

我猜你需要添加一个 required: false,在'include' object 中为 tagValue 添加一个 where 子句。 这样,当值为 1 时,它会带来生态系统。

 Models.ecosystem.hasOne(Models.appMetadata, { foreignKey: 'tagValue' }); Models.appMetadata.belongsTo(Models.ecosystem, { foreignKey: 'tagValue' }); const options = { where: { accountId }, include: [ { model: Models.appMetadata, include: [{ model: Models.ecosystem, required: false, where: {tagValue: 1}}], } ] }; const apps = await Models.app.findAll(options);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM