Sequelize不使用模型中的字段别名

时间:2018-09-17 16:36:16

标签: sql node.js postgresql sequelize.js

我有一个名为proxyPool的模型,其中包含以下字段:

poolId: {
    type: DataTypes.INTEGER,
    references: {
        model: 'pool',
        key: 'id',
    },
    field: 'pool_id',
},
proxyId: {
    type: DataTypes.INTEGER,
    references: {
        model: 'proxy',
        key: 'id',
    },
    field: 'proxy_id',
},

这是一个N:M表,其中包含两个具有下一个关联的表:

proxy.associate = (models) => {
    proxy.belongsToMany(models.pool, {
        through: models.proxyPool,
        foreignKey: 'proxy_id',
    });
};

pool.associate = (models) => {
    pool.belongsToMany(models.proxy, {
        through: models.proxyPool,
        foreignKey: 'pool_id',
    });
};

当我调用proxyPool.findOrCreate({where: {proxyId, poolId}})时,它说不存在proxyPool.proxyId列,但是在原始SQL中,我看到:

SELECT "id", "pool_id" AS "poolId", "createdAt", "updatedAt", "pool_id", "proxy_id" 
    FROM "portnoi"."proxy_pool" AS "proxyPool" 
    WHERE "proxyPool"."proxyId" = '3' AND "proxyPool"."pool_id" = '1' LIMIT 1;

为什么它为poolId = pool_id使用别名,而不为proxyId = proxy_id使用模型中描述的别名?

1 个答案:

答案 0 :(得分:0)

您是否尝试过这样的查询?

proxyPool.findOrCreate({
    where: {
        proxyId: {
            [Op.eq]: proxyId,
        }, 
        ...
    }
})