【问题标题】:SQLite SequelizeJs adding an extra filed in querySQLite Sequelize Js 在查询中添加一个额外的字段
【发布时间】:2018-09-11 20:51:02
【问题描述】:

我正在使用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 字段时,此查询工作正常

【问题讨论】:

  • 由于 models.product.belongsTo(models.user) 续集在产品模型上创建外键 userId。当您在模型上使用任何方法时,例如 findAll findOne 和其他方法,sequelize 模型会根据定义的属性和关联中的外键构造查询。

标签: node.js sqlite sequelize.js


【解决方案1】:

那是因为这条线:

此行将向产品添加一个 userId 属性以保存产品的主键值

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-04
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多