Sequelize Query Failing

时间:2017-12-25 09:10:19

标签: node.js sequelize.js

我有一个相同模型关联两次的模型。以下是模型定义

const RiStockSplitType = sequelize.define("RiStockSplitType",
    // Columns
    base.addColumns({
        "id": {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
        "splitType": {type: DataTypes.INTEGER, defaultValue: 1, field: "split_type"}
    }, DataTypes),
    // Configs
    new base.DefaultModelConfig("table")
);
RiStockSplitType.associate = function (models, value) {
    value.belongsTo(models.ReservationInfo, {
        foreignKey: {
            allowNull: false,
            name:"reservation_info_id"
        },
        as: "reservation_info"
    });
    value.belongsTo(models.ReservationInfo, {
        foreignKey: {
            allowNull: false,
            name:"split_type_ri_id"
        },
        as:"split_type_ri"
    });
};
return RiStockSplitType;

下面是我正在解雇的SequelizeQuery

 RiStockSplitType.findAll({
        include: [{
            model: ReservationInfo,
            as:"reservation_info"
        }],
    })
)}).then(function(){console.log(arguments)}).catch(err => {console.error('ERROR:', err);});

上面生成的SQL查询是

Executing (default): SELECT `RiStockSplitType`.`id`, `RiStockSplitType`.`split_type` AS `splitType`, `RiStockSplitType`.`created_on` AS `createdOn`, `RiStockSplitType`.`updated_on` AS `updatedOn`, `RiStockSplitType`.`reservation_info_id`, `RiStockSplitType`.`split_type_ri_id`, `reservation_info`.`id` AS `reservation_info.id`, `reservation_info`.`created_on` AS `reservation_info.createdOn`, `reservation_info`.`updated_on` AS `reservation_info.updatedOn`, `reservation_info`.`city_id` AS `reservation_info.city_id` FROM `warehouse_ristocksplittype` AS `RiStockSplitType` LEFT OUTER JOIN `warehouse_reservation_info` AS `reservation_info` ON `RiStockSplitType`.`reservation_info_id` = `reservation_info`.`id`;

我得到的错误是

  SequelizeDatabaseError: Unknown column 'reservation_info.city_id' in 'field list'

我正在使用Sequelize v4和Node 6.9.5。 我无法弄清楚错误的原因。错误中提到的列是数据库中的reservation_info.city_id

修改

ReservationInfo的模型定义

const ReservationInfo = sequelize.define("ReservationInfo",
    // Columns
    base.addColumns({
        "id": {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    }, DataTypes),
    // Configs,
    new base.DefaultModelConfig("warehouse_reservationinfo"));

ReservationInfo.associate = function (models, value) {
    value.belongsTo(models.City, {
        foreignKey: {
            allowNull: false,
            name: 'city_id'
        }
    });
};
return ReservationInfo;

1 个答案:

答案 0 :(得分:1)

查询正在加入warehouse_reservation_info,而传递给DefaultModelConfig的名称是warehouse_reservationinfo。表名之间不匹配。