<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ### 一對多 多對一 關聯模型 描述: 一對多: 一個部門有很多員工,但一個員工只能從屬于一個部門 多對一: 多個員工只能屬于一個部門 ***** 示例: ![](https://box.kancloud.cn/b2542fd7e69d1115f6df51b6c57d6b2b_543x230.png) department 部門表 employee 員工表 ***** ***** ### egg-sequelize 實現一對多 分類表: ![](https://box.kancloud.cn/b18d04fcc70fe07790339ab065b91830_133x122.png) 商品表: ![](https://box.kancloud.cn/1b436b5196d8391cccbb1e51da521899_260x176.png) 分類 1------n 商品 1. model 里面建2張模型,分別是category.js goods.js 2. catrgory.js 模型代碼: ~~~ module.exports = app => { const {Sequelize} = app const {STRING,INTEGER} = Sequelize const Category = app.model.define('category', { id: { type: INTEGER, primaryKey: true, autoIncrement: true }, title: { type: STRING } }, { timestamps: false , paranoid: false}); // 分類表關聯商品表 1:n Category.associate = function() { app.model.Category.hasMany(app.model.Goods, { as: 'goods', foreignKey: 'cate_id', targetKey: 'id'}); }; return Category; } ~~~ 3. goods.js 模型代碼: ~~~ module.exports = app => { const {Sequelize} = app const {STRING,INTEGER} = Sequelize const Goods = app.model.define('goods', { id: { type: INTEGER, primaryKey: true, autoIncrement: true }, title: { type: STRING }, price: {type: INTEGER}, cate_id: {type: INTEGER} }, { timestamps: false , paranoid: false}); // 商品表從屬分類 n-1 Goods.associate = function() { app.model.Goods.belongsTo(app.model.Category, { as: 'cate', foreignKey: 'cate_id', targetKey: 'id'}); }; return Goods; } ~~~ 4. controller 查詢 ~~~ const {Op} = this.app.Sequelize; const {Sequelize} = this.app; // 查詢所有分類下對應的所有商品 分類 1----n 商品 const cate = await this.ctx.model.Category.findAll({ include: [{ model: this.ctx.model.Goods, as: 'goods', attributes:[[Sequelize.fn('COUNT', Sequelize.col('*')), 'total_goods']] }], attributes: ['title'], group: 'title', distinct: true }); // 查詢商品所對應的分類 商品 n-----1 分類 const goods = await this.ctx.model.Goods.findAll({ include: [{ model: this.ctx.model.Category, as: 'cate' }], distinct: true }) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看