【问题标题】:Sequelizejs returns id besides the object for associated table除了关联表的对象之外,Sequelizejs 返回 id
【发布时间】:2019-01-03 10:50:39
【问题描述】:
class User extends Sequelize.Model {
    static init(sequelize, DataTypes) {
        return super.init(
            {
                id: {type: DataTypes.INTEGER(11), allowNull: false, autoIncrement: true, primaryKey: true},
                cityId: {
                    type: DataTypes.INTEGER(11).UNSIGNED,
                    field: 'city_id',
                    references: {
                        model: City,
                        key: 'city_id'
                    },
                    allowNull: false, defaultValue: 0
                }
            },
            {
                tableName: "users",
                timestamps: false,
                sequelize
            }
        );
    }

    static getByIdWithCity(id) {
        return this.findOne({
            where: {
                id: {
                    [Op.eq]: id
                }
            },
            include: [{model: City}],
            raw: false
        });
    }
}

class City extends Sequelize.Model {
    static init(sequelize, DataTypes) {
        return super.init(
            {
                cityId: {type: DataTypes.INTEGER(11).UNSIGNED, allowNull: false, autoIncrement: true, primaryKey: true, field: 'city_id'},
                countryId: {type: DataTypes.INTEGER(11).UNSIGNED, allowNull: false, default: 0, field: 'country_id'}
            },
            {
                tableName: "city",
                timestamps: false,
                sequelize
            }
        );
    }
}

User.belongsTo(City, {foreignKey: 'cityId', targetKey: 'cityId'});
City.hasOne(User, {foreignKey: 'cityId', sourceKey: 'cityId'});

getByIdWithCity 返回:

{
    "id": 15,
    "cityId": 3538,
    "City": {
        "cityId": 3538,
        "countryId": 4
    }
}

为什么返回cityId

当然我可以排除这些字段:

static getByIdWithCity(id) {
    return this.findOne({
        where: {
            id: {
                [Op.eq]: id
            }
        },
        attributes: { exclude: ['cityId'] },
        include: [{
            model: City,
        }],
        raw: false
    });
}

但这是正确的方法吗?

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    它返回cityId,因为它是在您的模型中定义的。

    是的exclude 会很好地做到这一点,实现相同结果的另一种方法是使用attributes 选项(基本上是include)明确写入您想要的属性:

    static getByIdWithCity(id) {
        return this.findOne({
            where: {
                id: {
                    [Op.eq]: id
                }
            },
            attributes: ['id'],
            include: [{
                model: City,
            }],
            raw: false
        });
    }
    

    所以是一样的:

    attributes: {
      include: ['id']
    }
    

    至于如何以“正确的方式”,我认为这取决于:

    exclude 可以在您想从查询中返回多个属性时派上用场,例如,假设User 模型总共有 13 个属性,并且您想要除其中 2 个之外的所有属性,因为在这种情况下,只使用exclude 而不是使用attributes 选项显式写入所有11 个属性更有意义。

    反之亦然:
    如果您只想返回 13 个属性中的 2 个,则使用 attributes 选项显式编写您想要的属性会派上用场,在这种情况下,显式编写它们比排除其余 11 个属性更有意义。

    在您的情况下,您只有一个属性id,而您只想排除一个属性cityId,无论您使用include 还是exclude,因为它们将运行相同的查询:SELECT id ..,您可以从模型定义中删除cityId(如果您不需要它),但将其保留在迁移中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多