代码之家  ›  专栏  ›  技术社区  ›  Suvitruf - Andrei Apanasik

sequelizejs除了返回关联表的对象外,还返回id

  •  0
  • Suvitruf - Andrei Apanasik  · 技术社区  · 7 年前
    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
        });
    }
    

    但这是正确的方式吗?

    1 回复  |  直到 7 年前
        1
  •  -1
  •   mohdule    7 年前

    它又回来了 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']
    }
    

    至于如何做到“正道”,我认为这取决于:

    排除 当您希望从查询返回不止一个属性时,可以很方便地使用,例如,假设 User 模型共有13个属性,除了其中的2个之外,您希望它们都是,在这种情况下,使用 排除 而不是使用 属性 选择。

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

    在这里,你只有一个属性 id 你只想排除一个属性 城市 不管你用什么 包括 排除 因为它们将运行相同的查询: SELECT id .. ,您可以移除 城市 从模型定义(如果不需要的话)开始,但要在迁移中保留它。

    推荐文章