SQLite SequelizeJs在查询中添加了额外的字段

时间:2018-04-02 16:06:53

标签: node.js sqlite sequelize.js

我正在使用sequelizeSQLite3。当我在我的代码中使用该模型时,它会生成错误的查询。任何人都可以帮我解决这个问题

这是我的模型定义

module.exports = function(sequelize, DataTypes) {

    let product = sequelize.define('product', {
        id: {
            type: DataTypes.INTEGER(10).UNSIGNED,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(200),
        },
        code: {
            type: DataTypes.STRING(100),
        },
        desc: {
            type: "BLOB",
        },
        productCategoryId: {
            type: DataTypes.INTEGER(10).UNSIGNED,
            references: {
                model: 'product_category',
                key: 'id'
            }
        },
        costPrice: {
            type: DataTypes.FLOAT,
        },
        sellPrice: {
            type: DataTypes.FLOAT,
        },
        markup: {
            type: DataTypes.FLOAT,
        },
        markupType: {
            type: DataTypes.ENUM('AMOUNT','PERCENTAGE'),
        },
        imgAttachment: {
            type: DataTypes.INTEGER(1),
        },
        minOrderQuantity: {
            type: DataTypes.INTEGER(10),
        },
        minStockQuantity: {
            type: DataTypes.INTEGER(10),
        },
        isComposite: {
            type: DataTypes.INTEGER(1),
        },
        isAllowedOutOfStockSale: {
            type: DataTypes.INTEGER(1),
            defaultValue: '0'
        },
        isActive: {
            type: DataTypes.INTEGER(1),
            defaultValue: '0'
        },
        isDeceptive: {
            type: DataTypes.INTEGER(1),
            defaultValue: '0'
        },
        createdAt: {
            type: DataTypes.DATE,
        },
        createdBy: {
            type: DataTypes.INTEGER(10).UNSIGNED,
            references: {
                model: 'user',
                key: 'id'
            }
        },
        deletedAt: {
            type: DataTypes.DATE,
        },
        deletedBy: {
            type: DataTypes.INTEGER(10).UNSIGNED,
            references: {
                model: 'user',
                key: 'id'
            }
        },
        updatedAt: {
            type: DataTypes.DATE,
        },
        updatedBy: {
            type: DataTypes.INTEGER(10).UNSIGNED,
            references: {
                model: 'user',
                key: 'id'
            }
        }
    }, {
        tableName: 'product',
        timestamps: false,
        defaultScope: {
            where: {
                isActive: true,
                deletedAt: null,
            }
        }
    });

    // Association
    product.associate = function(models) {
        models.product.belongsTo(models.user);
        models.product.belongsTo(models.user);
        models.product.belongsTo(models.user);
        models.product.belongsTo(models.product_category);
        models.product.hasMany(models.product_composition);
    };

    return product;
}

这是我的模型实施

models.findAll({})
    .then(data => {
        console.log(data)
    .catch(err => {
        console.log(err)
    });

我收到了SequelizeDatabaseError。经过调查,我会追踪生成的查询

SELECT `id`, `name`, `code`, `desc`, `productCategoryId`, `costPrice`, `sellPrice`, `markup`, `markupType`, `imgAttachment`, `minOrderQuantity`, `minStockQuantity`, `isComposite`, `isAllowedOutOfStockSale`, `isActive`, `isDeceptive`, `createdAt`, `createdBy`, `deletedAt`, `deletedBy`, `updatedAt`, `updatedBy`, `userId` FROM `product` AS `product` WHERE `product`.`id` = 1 AND `product`.`isActive` = 1 AND `product`.`deletedAt` IS NULL;

为什么在查询中添加userId。当我从此生成的查询中删除userId字段时,此查询正常工作

1 个答案:

答案 0 :(得分:0)

这是因为这一行:

此行将向product添加userId属性以保存Product

的主键值
models.product.belongsTo(models.user); 

但是这不会添加字段,原因是,外键名productCategoryId遵循命名约定,但在上面的情况下没有,

models.product.belongsTo(models.product_category); 

为此您应该明确定义,并且您还应该为关联添加别名,因为您正在使用一个表来表示3个关系,例如

models.product.belongsTo(models.user , { as : 'delete_by' ,foreignKey: 'deletedBy'} );
models.product.belongsTo(models.user , { as : 'created_by' ,foreignKey: 'createdBy'} );
models.product.belongsTo(models.user , { as : 'updated_by' , foreignKey: 'updatedBy'} );

有关详情: DO READ