2017-04-17 49 views
1

在購物車管理系統中, 用戶具有多個訂單並且訂單屬於一個用戶,在這種情況下,用戶ID('用戶'表'的PK)在'訂單'表中爲FK。 我試過這個,但是這給了關聯錯誤,訂單沒有關聯到用戶。續訂關聯 - 訂單未與用戶關聯

db.user.findAll({ 
    include: [ 
     { 
     model: db.order 
     } 
    ] 
    }) 
    .then(users => { 
     res.json(users) 
    }); 

下面是型號: -

訂量: -

'use strict'; 
var db = require('../models/index'); 

module.exports = function (sequelize, DataTypes) { 
    var order = sequelize.define('order', { 
    id: { 
     type: DataTypes.INTEGER, 
     allowNull: false, 
     primaryKey: true, 
     autoIncrement: true 
    }, 
    userId: { 
     type: DataTypes.INTEGER, 
     allowNull: false 
    }, 
    totalAmount: { 
     type: DataTypes.INTEGER, 
     allowNull: false 
    } 
    , 
    createdAt: { 
     type: DataTypes.DATE, 
     allowNull: true 
    }, 
    updatedAt: { 
     type: DataTypes.DATE, 
     allowNull: true 
    } 
    }, { 
     classMethods: { 
     associate: function (models) { 
      order.belongsTo(models.user); 
      order.belongsToMany(models.products, { through: { model: models.orderProduct } }); 
      // order.hasMany(models.orderProduct); 
     } 
     } 
    }); 
    return order; 
}; 

用戶: -

'use strict'; 
var db = require('../models/index'); 

module.exports = function (sequelize, DataTypes) { 

    var user = sequelize.define('user', { 
    id: { 
     type: DataTypes.INTEGER(11), 
     allowNull: false, 
     primaryKey: true, 
     autoIncrement: true 
    }, 
    name: { 
     type: DataTypes.STRING(255), 
     allowNull: false 
    }, 
    email: { 
     type: DataTypes.STRING(255), 
     allowNull: false 
    }, 
    password: { 
     type: DataTypes.STRING(255), 
     allowNull: false 
    }, 
    address: { 
     type: DataTypes.STRING(255), 
     allowNull: true 
    }, 
    contact: { 
     type: DataTypes.STRING(255), 
     allowNull: true 
    }, 
    type: { 
     type: DataTypes.STRING(255), 
     allowNull: true 
    }, 
    createdAt: { 
     type: DataTypes.DATE, 
     allowNull: true 
    }, 
    updatedAt: { 
     type: DataTypes.DATE, 
     allowNull: true 
    }, 
    }, { 
     activated: { 
     type: DataTypes.BOOLEAN, 
     allowNull: true 
     } 
    }, { 
     classMethods: { 
     associate: function (models) { 
      user.hasMany(models.order); 
     } 
     } 
    }); 
    return user; 
}; 

以下是錯誤: -

Unhandled rejection Error: order is not associated to user! 
    at Model.validateIncludedElement (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:558:11) 
    at F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:440:29 
    at Array.map (native) 
    at Model.validateIncludedElements (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:436:37) 
    at Model.<anonymous> (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:1372:32) 
    at Model.tryCatcher (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\util.js:16:23) 
    at Promise._settlePromiseFromHandler (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:512:31) 
    at Promise._settlePromise (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:569:18) 
    at Promise._settlePromise0 (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:614:10) 
    at Promise._settlePromises (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:693:18) 
    at Async._drainQueue (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\async.js:133:16) 
    at Async._drainQueues (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\async.js:143:10) 
    at Immediate.Async.drainQueues (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\async.js:17:14) 
    at runCallback (timers.js:666:20) 
    at tryOnImmediate (timers.js:639:5) 
    at processImmediate [as _immediateCallback] (timers.js:611:5) 

回答

1

指定您的外鍵。您正在使用的這些鍵不會被自動推斷出來。

order.belongsTo(models.user, {foreignKey: 'userID'}); 
.. 
user.hasMany(models.order, {foreignKey: 'userID'}); 

編輯:我重新檢查了你的帖子,你的用戶是以錯誤的方式定義的。您的最後一個字段「激活」在外面,關閉前一個對象。這就是爲什麼它不讀你的關聯。

updatedAt: { 
     type: DataTypes.DATE, 
     allowNull: true 
    }, 
    }, { 
     activated: { 
     type: DataTypes.BOOLEAN, 
     allowNull: true 
     } 
    }, { 
     classMethods: { 
     associate: function (models) { 
      user.hasMany(models.order); 
     } 
     } 
    }); 

應該是:

 updatedAt: { 
      type: DataTypes.DATE, 
      allowNull: true 
     }, 
     activated: { 
      type: DataTypes.BOOLEAN, 
      allowNull: true 
      } 
     }, { 
      classMethods: { 
      associate: function (models) { 
       user.hasMany(models.order); 
      } 
      } 
     }); 
+0

仍然沒有工作 –

+0

檢查我的編輯。你在定義結構中有錯誤 – yBrodsky

+1

謝謝你,你是一個救命的人:) –

0

試試這個。 修改您的協會是這樣的:

order.belongsTo(models.user, {as : 'User'}); 

user.hasMany(models.order, {as : 'Orders'}); 

雖然查詢:

db.user.findAll({ 
include: [{model: db.order, as: 'Orders'}] 
}) 
.then(users => { 
    res.json(users) 
}); 
+0

問題出在我的user.js(激活屬性),謝謝:) –