【问题标题】:Sequelize finders excludes attributes but want allSequelize finder 排除属性但想要所有
【发布时间】:2020-10-21 10:04:30
【问题描述】:

我在 nodeJs 中使用 sequelize。 我有一个带有外键的表,但我不知道为什么,它们没有出现在查询中。

import { Model, DataTypes } from "sequelize"
import { sequelize } from "./config"

class VolumeRangeDiscount extends Model{
    public readonly id: number;
    public readonly updatedAt!:Date;
    public readonly createdAt!:Date;
    public discount: number;

    public static associate(models){
        VolumeRangeDiscount.belongsTo(models.VolumeRange, {
            foreignKey: "volumeRangeId",
            as: "volumeRange",
        });
        VolumeRangeDiscount.belongsTo(models.Pack, {
            foreignKey: "packId",
            as: "pack",
        });
    }
}

VolumeRangeDiscount.init({
    id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: DataTypes.INTEGER,
    },
    createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
    },
    updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
    },
    discount:{
        type: DataTypes.INTEGER,
        allowNull: true,
    }
    
},  { sequelize, tableName: "VolumeRangeDiscount" })



export default VolumeRangeDiscount

当我想通过 id 查找一个时,它不会查询外键列:

export function findVolumeRangeDiscountById(id) {
    return VolumeRangeDiscount.findOne({ where: { id }, raw: true });
}

而生成的查询是:

SELECT "id", "createdAt", "updatedAt", "discount" FROM "VolumeRangeDiscount" AS "VolumeRangeDiscount" WHERE "VolumeRangeDiscount"."id" = '3';

但是,如果我添加带有列名的属性选项,它会起作用,它们会被返回。 您知道为什么默认情况下它们不在生成的查询中吗?

它没有在我的模型中添加foreignKey,VolumeRangeDiscount.rawAttributes 返回:

 {
    id: {allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: INTEGER {
    options: {
    },
    _length: undefined,
        _zerofill: undefined,
        _decimals: undefined,
        _precision: undefined,
        _scale: undefined,
        _unsigned: undefined},
    Model: VolumeRangeDiscount,
        fieldName: 'id',
        _modelAttribute: true,
        field: 'id'
},
createdAt: {type: DATE {options: [Object], _length: ''
},
    allowNull: false,
        Model: VolumeRangeDiscount,
        fieldName: 'createdAt',
        _modelAttribute: true,
        field: 'createdAt'},
updatedAt: { type: DATE {options: [Object], _length: ''
},
    allowNull: false,
        Model: VolumeRangeDiscount,
        fieldName: 'updatedAt',
        _modelAttribute: true,
        field: 'updatedAt'},
discount: {type: INTEGER {
    options: {},
    _length: undefined,
        _zerofill: undefined,
        _decimals: undefined,
        _precision: undefined,
        _scale: undefined,
        _unsigned: undefined
},
    allowNull: true,
        Model: VolumeRangeDiscount,
        fieldName: 'discount',
        _modelAttribute: true,
        field: 'discount'},
}

【问题讨论】:

  • 您只需要根据@tbking 回答的代码结构使用包含函数

标签: node.js sequelize.js


【解决方案1】:

Sequelize 是一个 ORM,它会为您处理关联。您可以通过两种方式使用它来获取关联的行,而不是获取外键:

  1. 渴望加载

    VolumeRangeDiscount.findOne({ where: { id }, include 'VolumeRange' });
    

    这将使用关联的行填充VolumeRange 对象。

  2. 延迟加载

     const vrDiscount = VolumeRangeDiscount.findOne({ where: { id }, include 'VolumeRange' });
     const volumeRange = vrDiscount.getVolumeRange();
    

更多信息请见docs here

【讨论】:

  • 感谢您的回答,但它不起作用,因为 Sequelize 不会自动向我的模型添加属性(您可以查看我的编辑)。
猜你喜欢
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 2016-12-13
  • 2012-01-08
  • 2021-10-22
  • 2012-10-06
  • 1970-01-01
  • 2016-02-19
相关资源
最近更新 更多