{"action":"created","issue":{"url":"https://api.github.com/repos/eggjs/egg/issues/2224","repository_url":"https://api.github.com/repos/eggjs/egg","labels_url":"https://api.github.com/repos/eggjs/egg/issues/2224/labels{/name}","comments_url":"https://api.github.com/repos/eggjs/egg/issues/2224/comments","events_url":"https://api.github.com/repos/eggjs/egg/issues/2224/events","html_url":"https://github.com/eggjs/egg/issues/2224","id":305878298,"number":2224,"title":"为什么用sequelize做关联查询的时候，查出来的结果包含关系表数据","user":{"login":"zsbox","id":20392882,"avatar_url":"https://avatars0.githubusercontent.com/u/20392882?v=4","gravatar_id":"","url":"https://api.github.com/users/zsbox","html_url":"https://github.com/zsbox","followers_url":"https://api.github.com/users/zsbox/followers","following_url":"https://api.github.com/users/zsbox/following{/other_user}","gists_url":"https://api.github.com/users/zsbox/gists{/gist_id}","starred_url":"https://api.github.com/users/zsbox/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zsbox/subscriptions","organizations_url":"https://api.github.com/users/zsbox/orgs","repos_url":"https://api.github.com/users/zsbox/repos","events_url":"https://api.github.com/users/zsbox/events{/privacy}","received_events_url":"https://api.github.com/users/zsbox/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":{"login":"jtyjty99999","id":893152,"avatar_url":"https://avatars0.githubusercontent.com/u/893152?v=4","gravatar_id":"","url":"https://api.github.com/users/jtyjty99999","html_url":"https://github.com/jtyjty99999","followers_url":"https://api.github.com/users/jtyjty99999/followers","following_url":"https://api.github.com/users/jtyjty99999/following{/other_user}","gists_url":"https://api.github.com/users/jtyjty99999/gists{/gist_id}","starred_url":"https://api.github.com/users/jtyjty99999/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jtyjty99999/subscriptions","organizations_url":"https://api.github.com/users/jtyjty99999/orgs","repos_url":"https://api.github.com/users/jtyjty99999/repos","events_url":"https://api.github.com/users/jtyjty99999/events{/privacy}","received_events_url":"https://api.github.com/users/jtyjty99999/received_events","type":"User","site_admin":false},"assignees":[{"login":"jtyjty99999","id":893152,"avatar_url":"https://avatars0.githubusercontent.com/u/893152?v=4","gravatar_id":"","url":"https://api.github.com/users/jtyjty99999","html_url":"https://github.com/jtyjty99999","followers_url":"https://api.github.com/users/jtyjty99999/followers","following_url":"https://api.github.com/users/jtyjty99999/following{/other_user}","gists_url":"https://api.github.com/users/jtyjty99999/gists{/gist_id}","starred_url":"https://api.github.com/users/jtyjty99999/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jtyjty99999/subscriptions","organizations_url":"https://api.github.com/users/jtyjty99999/orgs","repos_url":"https://api.github.com/users/jtyjty99999/repos","events_url":"https://api.github.com/users/jtyjty99999/events{/privacy}","received_events_url":"https://api.github.com/users/jtyjty99999/received_events","type":"User","site_admin":false}],"milestone":null,"comments":1,"created_at":"2018-03-16T10:35:56Z","updated_at":"2018-03-19T06:46:38Z","closed_at":null,"author_association":"NONE","body":"文章标签数据表tag, 字段 id, tagname\r\n文章数据表post 字段 id, title, user_id, content\r\n关系表tag_to_post 字段 id, post_id, tag_id\r\n关联写法\r\n```\r\n// model/post.js\r\napp.model.Post.belongsToMany(app.model.Tag, { as: 'tag', through: 'tag_to_post' , foreignKey: 'post_id' });\r\n// model/tag.js\r\napp.model.Tag.belongsToMany(app.model.Post, { as: 'posts', through: 'tag_to_post', foreignKey: 'tag_id' });\r\n// 查询\r\n async find(id) {\r\n    const post = await this.ctx.model.Post.findById(id, {\r\n      include: [{\r\n        model: this.ctx.model.User,\r\n        as: 'user',\r\n        attributes: [ 'id', 'name', 'age' ],\r\n      },\r\n      {\r\n        model: this.ctx.model.Tag,\r\n        as: 'tag',\r\n        attributes: [ 'id', 'tagname' ],\r\n      }\r\n    ],\r\n    });\r\n    if (!post) {\r\n      this.ctx.throw(404, 'post not found');\r\n    }\r\n    return post;\r\n  }\r\n// 结果\r\n{\r\n\"id\":2,\r\n\"title\":\"卡是几点回\",\r\n\"content\":\"啊就是大；滴蜡；开始\",\r\n\"user_id\":2,\r\n\"user\":{\"id\":2,\"name\":\"李四\",\"age\":29},\r\n\"tag\":[\r\n  {\r\n   \"id\":1,\r\n  \"tagname\":\"华为\",\r\n  \"tag_to_post\":{\"post_id\":2,\"tag_id\":1}\r\n  }\r\n]}\r\n\r\n里面包含了关系表的信息 我明明只查了tag数据 不想要tag_to_post的数据啊 \r\n是我哪里写的有问题么 请大神帮忙啊 怎么查才能去掉 另外关联写法应该没错吧？\r\n```"},"comment":{"url":"https://api.github.com/repos/eggjs/egg/issues/comments/374115926","html_url":"https://github.com/eggjs/egg/issues/2224#issuecomment-374115926","issue_url":"https://api.github.com/repos/eggjs/egg/issues/2224","id":374115926,"user":{"login":"MUHM","id":14190805,"avatar_url":"https://avatars3.githubusercontent.com/u/14190805?v=4","gravatar_id":"","url":"https://api.github.com/users/MUHM","html_url":"https://github.com/MUHM","followers_url":"https://api.github.com/users/MUHM/followers","following_url":"https://api.github.com/users/MUHM/following{/other_user}","gists_url":"https://api.github.com/users/MUHM/gists{/gist_id}","starred_url":"https://api.github.com/users/MUHM/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MUHM/subscriptions","organizations_url":"https://api.github.com/users/MUHM/orgs","repos_url":"https://api.github.com/users/MUHM/repos","events_url":"https://api.github.com/users/MUHM/events{/privacy}","received_events_url":"https://api.github.com/users/MUHM/received_events","type":"User","site_admin":false},"created_at":"2018-03-19T06:46:38Z","updated_at":"2018-03-19T06:46:38Z","author_association":"NONE","body":"async find(id) {\r\n  const post = await this.ctx.model.Post.findById(id, {\r\n    include: [{\r\n      model: this.ctx.model.User,\r\n      as: 'user',\r\n      attributes: ['id', 'name', 'age'],\r\n      through: {\r\n        attributes: [],\r\n      }\r\n    },\r\n    {\r\n      model: this.ctx.model.Tag,\r\n      as: 'tag',\r\n      attributes: ['id', 'tagname'],\r\n    }\r\n    ],\r\n  });\r\n  if (!post) {\r\n    this.ctx.throw(404, 'post not found');\r\n  }\r\n  return post;\r\n}\r\n试试"}}
{"actor":{"display_login":"MUHM"}}