0
我有兩個模型,policyTable
和policy_rule
定義如下。在policy_rule
中有一列叫做policy_id
,它是一個外鍵,在policyTable
中對id
列的引用。 policy_rule
可以有多個policyTable
,即有1:N的關係。續編簡單內連接
var policyTable = dbController.db.define('policyTable', {
id: {
type: Sequelize.BIGINT,
autoIncrement: true,
primaryKey: true
},
't': {
type: Sequelize.BIGINT,
unique: true
},
'name': {
type: Sequelize.STRING,
},
src: {
type: Sequelize.STRING
}
}, {
timestamps: false,
freezeTableName: true,
tableName: 'zo_policy',
});
var policy_rule = dbController.db.define('policy_rule', {
policy_id: {
type: Sequelize.BIGINT,
references: {
model: policyTable,
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
agent_id: {
type: Sequelize.BIGINT,
},
enabled: {
type: Sequelize.BOOLEAN,
}
}, {
timestamps: false,
freezeTableName: true,
tableName: 'zo_policy_rule',
});
現在我想加入這兩個模型,並獲得它們的所有列。我怎樣才能做到這一點?我想下面的代碼,但它說
Error: policy_rule is not associated to policyTable!
function getAllPolicies() {
return policyTable.findAndCountAll({
include: [{
model: policy_rule
}]
}).then(function (users) {
console.log(users);
data.count = users.count;
data.users = users.rows;
console.log(data);
return data;
});
};