多对多关系

时间:2018-07-09 09:54:29

标签: angularjs node.js orm sequelize.js

我陷入了多对多的续集关系,并以很少的测试模型进行描述。

const User1 = sequelize.define('user1', { name: Sequelize.STRING } )
const Account1 = sequelize.define('account1', { name: Sequelize.STRING } )
const UserRole1 = sequelize.define('user_role1', { name: Sequelize.STRING } )
const Token1 = sequelize.define('token1', { name: Sequelize.STRING }

有关系

Account1.belongsToMany(User1, {through: {model: UserRole1, unique: false}, foreignKey: 'account1_id'} )
User1.belongsToMany(Account1, {through: {model: UserRole1, unique: false}, foreignKey: 'user1_id'} )    
UserRole1.belongsTo(User1)
UserRole1.belongsTo(Account1)    
Account1.hasMany(UserRole1)
User1.hasMany(UserRole1)    
Token1.belongsTo(UserRole1)
UserRole1.hasMany(Token1)    
Token1.belongsTo(Account1)
Account1.hasMany(Token1)

当我在token1表上触发查询

Token1
    .findAll({
        where: {
            id: 1
        },
        include:[
            {
                model: UserRole1
            }
        ],
    })
    .then(function(table1Res){
        console.log(' -- table1Res: ' + JSON.stringify(table1Res));
    })

它标记错误token1.user_role1_account1_id does not exist,但是它应该已经在token1.user_role1_id = user_role1.id上用左联接触发了查询,但是它使用了 token1.user_role1_account1_id = user_role1.account1_id在左联接过程中属于CategoriesToMany约束。

谢谢。

0 个答案:

没有答案