无法运行sequeliz查询来从关联模型中获取行

问题描述 投票:0回答:1

我一直在尝试运行此查询来获取

PropValue
balance-prop
,其中
userId
等于
user.id
我的关系如下:

Prop 到 PropValue - 1-M

PropValue 到用户 - M-M 通过 UserPropValue

Prop 与 User 没有关联

有问题的查询:

    const balancePropValue = await db.PropValue.findOne({
        where: {
          include: [
            {
              model: db.UserPropValue,
              where: { userId: user.id },
              include: {
                model: db.User,
                where: { id: user.id }
              }
            },
            {
              model: db.Prop,
              where: { slug: "balance-slug" }
            }
          ]
        },
        attributes: ['value']
      });

续集关联:

prop.js
协会:

    static associate(models) {
      models.Role.hasOne(models.Prop, { foreignKey: 'roleId', constraints: true });
    }

propValue.js
协会:

    static associate(models) {
      models.Prop.hasMany(models.PropValue, { foreignKey: 'propId', constraints: true });
      models.PropValue.belongsToMany(models.User, { 
        through: models.UserPropValue,
        foreignKey: 'propValueId'
    });
    }

userPropValue.js
协会:

    static associate(models) {
      UserPropValue.belongsTo(models.User, { through:models.UserPropValue, foreignKey: 'userId', onDelete: 'SET NULL' });
      UserPropValue.belongsTo(models.PropValue, { through:models.UserPropValue, foreignKey: 'propValueId', onDelete: 'CASCADE' });
    }

user.js
协会:

    static associate(models) {
      models.User.belongsToMany(models.PropValue, { 
        through: models.UserPropValue,
        foreignKey: 'userId'
    });

我得到的错误:

Invalid value {\n  model: UserPropValue,\n  where: { userId: 1 },\n  include: { model: User, where: [Object] }\n}

还尝试使用 where 条件进行此查询:

const balancePropValue = await db.PropValue.findOne({
  where: {
    include: [
      {
        model: db.User,
        where: { id: user.id },
      },
      {
        model: db.Prop,
        where: { slug: "balance-slug" },
      }
    ]
  },
  attributes: ['value']
});

出现此错误:

Invalid value { model: User, where: { id: 1 } }
javascript sequelize.js associations
1个回答
0
投票

可能不是理想的解决方案,但我完全用它的 id 来拉动道具,之后函数结果如下:

async function systemToUserTransaction(params) {
    const user = await db.User.findOne({where: {phoneNumber:params.phoneNumber}});
    const prop = await db.Prop.findOne({where: {slug: "balance-prop"}});
    if(!user){
        throw 'User not found';
    }

    console.log(params.transactionType);
    if(params.transactionType !== "add" && params.transactionType !== "subtract") {
        throw 'No such transaction type exists';
    }
    balancePropValue = await db.PropValue.findOne({
        include: db.User,
        where:{
            id: user.id
        },
        include: db.Prop,
        where:{
            propId: prop.id
        } 
    }) 

    console.log(balancePropValue);

    value = (Number(balancePropValue.value) + Number(params.value)).toString();
    balancePropValue.value = value;

    console.log(balancePropValue.value);

    const transaction = await new db.Transaction({
        userId: user.id,
        transactionType: params.transactionType,
        value: params.value
    })

    balancePropValue.save();
    transaction.save();
}
© www.soinside.com 2019 - 2024. All rights reserved.