• <legend id='1lT3d'><style id='1lT3d'><dir id='1lT3d'><q id='1lT3d'></q></dir></style></legend>
  • <tfoot id='1lT3d'></tfoot>
    • <bdo id='1lT3d'></bdo><ul id='1lT3d'></ul>

        <small id='1lT3d'></small><noframes id='1lT3d'>

        <i id='1lT3d'><tr id='1lT3d'><dt id='1lT3d'><q id='1lT3d'><span id='1lT3d'><b id='1lT3d'><form id='1lT3d'><ins id='1lT3d'></ins><ul id='1lT3d'></ul><sub id='1lT3d'></sub></form><legend id='1lT3d'></legend><bdo id='1lT3d'><pre id='1lT3d'><center id='1lT3d'></center></pre></bdo></b><th id='1lT3d'></th></span></q></dt></tr></i><div id='1lT3d'><tfoot id='1lT3d'></tfoot><dl id='1lT3d'><fieldset id='1lT3d'></fieldset></dl></div>

        如何避免包含外键和包含对象的 Sequelize JS 关联

        How to Avoid Sequelize JS Associations Including both the Foreign Key and the Included Object(如何避免包含外键和包含对象的 Sequelize JS 关联)

          <small id='32l7d'></small><noframes id='32l7d'>

            <tfoot id='32l7d'></tfoot>
                <tbody id='32l7d'></tbody>

              <legend id='32l7d'><style id='32l7d'><dir id='32l7d'><q id='32l7d'></q></dir></style></legend>
                <bdo id='32l7d'></bdo><ul id='32l7d'></ul>
                <i id='32l7d'><tr id='32l7d'><dt id='32l7d'><q id='32l7d'><span id='32l7d'><b id='32l7d'><form id='32l7d'><ins id='32l7d'></ins><ul id='32l7d'></ul><sub id='32l7d'></sub></form><legend id='32l7d'></legend><bdo id='32l7d'><pre id='32l7d'><center id='32l7d'></center></pre></bdo></b><th id='32l7d'></th></span></q></dt></tr></i><div id='32l7d'><tfoot id='32l7d'></tfoot><dl id='32l7d'><fieldset id='32l7d'></fieldset></dl></div>

                  本文介绍了如何避免包含外键和包含对象的 Sequelize JS 关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何避免同时显示 sequelize 创建的 foreignKey 和通过包含急切获取的对象?

                  How can I avoid showing both the foreignKey that sequelize creates and the eagerly fetched object through includes?

                  我有以下模型结构:

                  表单条目:

                  • 所有者:用户
                  • 创建者:用户
                  • 修改者:用户
                  • formEntryData:[FormEntryData]

                  我在阅读了 SequelizeJS 文档后对其进行了建模,并提出了以下建议:

                  I modeled it after reading through SequelizeJS docs and came up with the following:

                  const User = sequelize.define('user', {
                      id: {
                          type: Sequelize.BIGINT(20),
                          field: 'user_id',
                          primaryKey: true
                      },
                      emailAddress: {
                          type: Sequelize.STRING(256),
                          field: 'email_address'
                      }
                  }, {
                      tableName: 'users',
                      timestamps: false
                  });
                  
                  const FormEntryData = sequelize.define('formEntryData', {
                      id: {
                          type: Sequelize.BIGINT(20),
                          field: 'id',
                          primaryKey: true
                      },
                      entryId: {
                          type: Sequelize.BIGINT(20),
                          field: 'entry_id'
                      },
                      ...
                  }, {
                      tableName: 'formEntryData',
                      timestamps: false
                  });
                  
                  const FormEntry = sequelize.define('formEntry', {
                      id: {
                          type: Sequelize.BIGINT(20),
                          field: 'entry_id',
                          primaryKey: true
                      },
                      ...
                  }, {
                      tableName: 'formEntries',
                      timestamps: false
                  });
                  

                  然后我需要创建关联以将模型联系在一起,经过大量试验和错误后,我得出以下结论:

                  I then need to create the associations to tie the models together and after a lot of trial and error I came up with the following:

                  FormEntry.hasMany(FormEntryData, {foreignKey: 'entry_id', as: 'FormEntryData'});
                  FormEntry.belongsTo(User, {foreignKey: 'created_by', as: 'CreatedBy'});
                  FormEntry.belongsTo(User, {foreignKey: 'modified_by', as: 'ModifiedBy'});
                  FormEntry.belongsTo(User, {foreignKey: 'owner', as: 'Owner'});
                  

                  然后我可以通过执行以下操作来查询数据:

                  I then was able to query the data by doing the following:

                  FormEntry.findByPrimary(1472280, {
                      include: [
                          {
                              model: FormEntryData,
                              as: "FormEntryData"
                          },
                          {
                              model: User,
                              as: "CreatedBy"
                          },
                          {
                              model: User,
                              as: "Owner"
                          },
                          {
                              model: User,
                              as: "ModifiedBy"
                          }
                      ]
                  })
                  

                  不幸的是,我的结果似乎有点重复,因为它似乎包括外键和急切获取的对象.

                  Unfortunately, my results seem kind of repetitive as it seems to be including both the foreign key and the object that is eagerly fetched.

                  {
                      "id": 1472280,
                      ...
                      "created_by": 26508,  <-- repetitive (don't want this)
                      "modified_by": 26508, <-- repetitive (don't want this)
                      "owner": null,        <-- repetitive (don't want this)
                      "FormEntryData": [
                          {
                              "id": 27164476,
                              "entryId": 1472280, <-- repetitive (but I want this one)
                              ...
                              "entry_id": 1472280 <-- repetitive (don't want this)
                          },
                          ...
                      ],
                      "CreatedBy": {        <-- repetitive (but I want this one)
                          "id": 26508,
                          "emailAddress": "swaraj.kler@greywallsoftware.com"
                      },
                      "Owner": null,        <-- repetitive (but I want this one)
                      "ModifiedBy": {       <-- repetitive (but I want this one)
                          "id": 26508,
                          "emailAddress": "swaraj.kler@greywallsoftware.com"
                      }
                  }
                  

                  推荐答案

                  需要从查询中排除指定字段

                  You need to exclude specified fields from the query

                  FormEntry.findByPrimary(1472280, {
                      include: [
                          {
                              model: FormEntryData,
                              as: "FormEntryData",
                              attributes: { exclude: ['entry_id'] }
                          },
                          {
                              model: User,
                              as: "CreatedBy"
                          },
                          {
                              model: User,
                              as: "Owner"
                          },
                          {
                              model: User,
                              as: "ModifiedBy"
                          }
                      ],
                      attributes: { exclude: ['owner', 'created_by', 'modified_by'] }
                  })
                  

                  这篇关于如何避免包含外键和包含对象的 Sequelize JS 关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                  <tfoot id='X8Fnn'></tfoot>
                    • <bdo id='X8Fnn'></bdo><ul id='X8Fnn'></ul>

                      <small id='X8Fnn'></small><noframes id='X8Fnn'>

                      <legend id='X8Fnn'><style id='X8Fnn'><dir id='X8Fnn'><q id='X8Fnn'></q></dir></style></legend>
                      <i id='X8Fnn'><tr id='X8Fnn'><dt id='X8Fnn'><q id='X8Fnn'><span id='X8Fnn'><b id='X8Fnn'><form id='X8Fnn'><ins id='X8Fnn'></ins><ul id='X8Fnn'></ul><sub id='X8Fnn'></sub></form><legend id='X8Fnn'></legend><bdo id='X8Fnn'><pre id='X8Fnn'><center id='X8Fnn'></center></pre></bdo></b><th id='X8Fnn'></th></span></q></dt></tr></i><div id='X8Fnn'><tfoot id='X8Fnn'></tfoot><dl id='X8Fnn'><fieldset id='X8Fnn'></fieldset></dl></div>
                          <tbody id='X8Fnn'></tbody>