【发布时间】:2023-01-30 13:52:23
【问题描述】:
我有这两个型号:
- 订购型号
- 解决方案模型
订单模型
'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class Orders extends Model { /** * Helper method for defining associations. * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */ static associate(models) { // define association here Orders.hasMany(models.Payments, { foreignKey: { name: 'order', allowNull: false, }, constraints: false, onDelete: 'cascade', }); Orders.hasOne(models.Solutions, { foreignKey: { name: 'order', allowNull: false, }, constraints: false, onDelete: 'cascade', as: "solution" }); } } Orders.init( { order_no: { defaultValue: DataTypes.UUIDV4, type: DataTypes.UUID, primaryKey: true, allowNull: false, unique: true, }, order_date: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, }, title: { type: DataTypes.STRING, allowNull: false, }, }, { sequelize, modelName: 'Orders', tableName: 'Orders', } ); return Orders; };#2。解决方案表
'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class Solutions extends Model { /** * Helper method for defining associations. * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */ static associate(models) { // define association here Solutions.belongsTo(models.Orders, { foreignKey: 'order', onDelete: 'cascade', constraints: false, as: "solution" }); } } Solutions.init( { solutionId: { defaultValue: DataTypes.UUIDV4, type: DataTypes.UUID, primaryKey: true, allowNull: false, unique: true, }, content: { type: DataTypes.TEXT, allowNull: false, }, additional_instruction: { type: DataTypes.TEXT, allowNull: true, }, date_submited: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, }, }, { sequelize, modelName: 'Solutions', } ); return Solutions; };我正在尝试获取解决方案尚未提交到解决方案表的所有订单,即订单字段(解决方案表中的外键)为空。
我试过这个
Orders.findAndCountAll({ include: [ { model: Users, attributes: ['username', 'email', 'uid'], }, { model: Solutions, as: "solution", where: { solutionId: { [Op.notIn]: Solutions.findAll({ attributes: ['solutionId'] }) } } } ], offset: page, limit, })我期望得到一个列表,其中包含未添加解决方案表中的解决方案的所有订单。我对续集有点陌生。
【问题讨论】:
标签: mysql node.js sequelize.js