如何在Sequelize中正确设置自定义“通过”表?

时间:2019-09-20 11:12:50

标签: mysql node.js sequelize.js associations

我正在尝试在使用Sequelize的项目中通过表建立自定义。我有一个Plannings表和一个Tasks表。现在,我希望实现一个将两者关联的表,但是我想添加一个自定义列“ done”。运行sync()时,似乎所有表都已正确设置。这是运行它以建立PlanningTask表的查询:

CREATE TABLE IF NOT EXISTS `PlanningTask` 
(`done` TINYINT(1) NOT NULL, `PlanningId` INTEGER(11) , `TaskId` INTEGER(11) ,
 PRIMARY KEY (`PlanningId`, `TaskId`), 
FOREIGN KEY (`PlanningId`) REFERENCES `Plannings` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, 
FOREIGN KEY (`TaskId`) REFERENCES `Tasks` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) 
ENGINE=InnoDB;

以下是当前型号: task.js

module.exports = (sequelize, DataTypes) => {
    const Task = sequelize.define('Task', {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            autoIncrement: true,
            primaryKey: true
        },
        type: {
            type: DataTypes.STRING,
            allowNull: false
        },
        description: {
            type: DataTypes.STRING,
            allowNull: false
        },
    })
    Task.associate = function(models) {
        Task.belongsToMany(models.Planning, {
            through: 'PlanningTask'
        })
    }
    // Methods
    return Task
}

planning.js

module.exports = (sequelize, DataTypes) => {
  const Planning = sequelize.define('Planning', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false
    }
    })
    Planning.associate = function (models) {
        Planning.belongsTo(models.User, {
            foreignKey: 'owner'
        })
        Planning.belongsToMany(models.Task, {
            through: 'PlanningTask'
        })
  }
  // Methods
  return Planning
}

planningtask.js

module.exports = (sequelize, DataTypes) => {
  const PlanningTask = sequelize.define('PlanningTask', {
    done: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    }
    }, {
        timestamps: false,
        freezeTableName: true       
    })
    PlanningTask.associate = function (models) {
        PlanningTask.belongsTo(models.Planning)
        PlanningTask.belongsTo(models.Task)
  }
  // Methods
  return PlanningTask
}

所有模型都加载了以下index.js文件:

const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const config = require('../config/config')
const db = {}

const sequelize = new Sequelize(
  config.db.database,
  config.db.user,
  config.db.password,
  config.db.options
)

fs
  .readdirSync(__dirname)
  .filter((file) =>
    file !== 'index.js'
  )
  .forEach((file) => {
    const model = sequelize.import(path.join(__dirname, file))
    db[model.name] = model
  })

Object.keys(db).forEach(function (modelName) {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db)
  }
})

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

我想知道订单是否可能存在一些问题(关联已在订单Planning,PlanningTask,Task中注册),但是当我更改订单时仍然无法使用。该表已正确创建,但是当我尝试使用Planning.add([task])输入Task时,它将运行以下查询:

SELECT `done`, `PlanningId`, `TaskId`
FROM `PlanningTask` AS `PlanningTask`
WHERE `PlanningTask`.`PlanningId` = 1 AND `PlanningTask`.`TaskId` IN (1);

因此,对于PlanningId,它尝试将'= 1'设置为正确,但是对于TaskId,它将使用'IN(1)',这是不正确的。在这种情况下,1是正确的任务ID。

在关联中我做错了什么吗,或者我尝试输入任务的方式是否有问题?

1 个答案:

答案 0 :(得分:0)

我的错误。 “完成”列仍设置为必填,在我的测试中,我没有为此发送任何值,其余的工作正常。