最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

多对多联结表中的多余列未在Sequelize中添加更新

IT培训 admin 13浏览 0评论

多对多联结表中的多余列未在Sequelize中添加/更新

这里是模型(很多对很多)

Company.js

module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    company_name: DataTypes.STRING,
  }, {
    timestamps: true,
    underscored: true,
    tableName: 'company',
  });
    Company.belongsToMany(models.Goal, {
      through: 'company_goal_relation', foreignKey: 'company_id', as: 'goal'
    });
  };
  return Company;
};

Goal.js

module.exports = (sequelize, DataTypes) => {
  const Goal = sequelize.define('Goal', {
    goal_name: DataTypes.STRING,
  }, {
    timestamps: false,
    underscored: true,
    tableName: 'goal',
  });
  Goal.associate = function(models) {
    Goal.belongsToMany(models.Company, {
      through: 'company_goal_relation',
      foreignKey: 'goal_id',
      as: 'company',
    });
  };
  return Goal;
};

CompanyGoalRelation.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const CompanyGoalRelation = sequelize.define('CompanyGoalRelation', {
    company_id: DataTypes.INTEGER,
    goal_id: DataTypes.INTEGER,
    objective: DataTypes.STRING,
    description: DataTypes.TEXT,
  }, {
    timestamps: false,
    underscored: true,
    tableName: 'company_goal_relation',
  });

  CompanyGoalRelation.associate = function(models) {
    CompanyGoalRelation.belongsTo(models.Company, { foreignKey: 'company_id' });
    CompanyGoalRelation.belongsTo(models.Goal, { foreignKey: 'goal_id' });
  };

  return CompanyGoalRelation;
};

我在这里想要添加或更新联结表的额外列。我尝试过的东西在下面。

const { goal, companyId } = req.body;
const company = await Company.findByPk(companyId);
    const goalInstance = await Goal.findOne({
      where: { id: goal.id },
    });
await company.addGoal(goalInstance, {
      through: {
        objective: goal.objective,
        description: goal.description,
      }
    });

未发生错误,并且成功添加了company_idgoal_id,但objectivedescription仍为null

我目前正在使用Sequelize(5.6.0)和PostgreSQL。有人可以帮忙吗?谢谢!

回答如下:

所以,正如我所说,在这方面,我不知道如何正确配置Sequelize,我认为这是浪费时间。我会做的只是执行原始查询。请注意,在您的情况下,您正在对数据库执行完全不必要的查询,首先按其ID提取Company记录,然后执行另一个查询来创建该关系(2个查询,而不是1个查询...)

因此,您可以将所需的关系字段(例如,从ajax请求中传递给您的快速应用程序(假设这是您正在使用的内容:):]]

const {companyId,goalId,objective,description} = req.body;

然后执行查询:

db.query(`insert into company_goal_relation (company_id,goal_id , objective, description,createdAt,updatedAt) values (?,?,?,?,NOW(),NOW()) `,{
        replacements:[companyId,goalId,objective,description]
    })

请注意,“ db”是您一开始就设置的序列化对象,您需要从config / setup文件中导入它。

当然,这不是执行此操作的“正常”续集方法,但我认为没有理由不应该这样做,然后结束了故事:D

多对多联结表中的多余列未在Sequelize中添加/更新

这里是模型(很多对很多)

Company.js

module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    company_name: DataTypes.STRING,
  }, {
    timestamps: true,
    underscored: true,
    tableName: 'company',
  });
    Company.belongsToMany(models.Goal, {
      through: 'company_goal_relation', foreignKey: 'company_id', as: 'goal'
    });
  };
  return Company;
};

Goal.js

module.exports = (sequelize, DataTypes) => {
  const Goal = sequelize.define('Goal', {
    goal_name: DataTypes.STRING,
  }, {
    timestamps: false,
    underscored: true,
    tableName: 'goal',
  });
  Goal.associate = function(models) {
    Goal.belongsToMany(models.Company, {
      through: 'company_goal_relation',
      foreignKey: 'goal_id',
      as: 'company',
    });
  };
  return Goal;
};

CompanyGoalRelation.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  const CompanyGoalRelation = sequelize.define('CompanyGoalRelation', {
    company_id: DataTypes.INTEGER,
    goal_id: DataTypes.INTEGER,
    objective: DataTypes.STRING,
    description: DataTypes.TEXT,
  }, {
    timestamps: false,
    underscored: true,
    tableName: 'company_goal_relation',
  });

  CompanyGoalRelation.associate = function(models) {
    CompanyGoalRelation.belongsTo(models.Company, { foreignKey: 'company_id' });
    CompanyGoalRelation.belongsTo(models.Goal, { foreignKey: 'goal_id' });
  };

  return CompanyGoalRelation;
};

我在这里想要添加或更新联结表的额外列。我尝试过的东西在下面。

const { goal, companyId } = req.body;
const company = await Company.findByPk(companyId);
    const goalInstance = await Goal.findOne({
      where: { id: goal.id },
    });
await company.addGoal(goalInstance, {
      through: {
        objective: goal.objective,
        description: goal.description,
      }
    });

未发生错误,并且成功添加了company_idgoal_id,但objectivedescription仍为null

我目前正在使用Sequelize(5.6.0)和PostgreSQL。有人可以帮忙吗?谢谢!

回答如下:

所以,正如我所说,在这方面,我不知道如何正确配置Sequelize,我认为这是浪费时间。我会做的只是执行原始查询。请注意,在您的情况下,您正在对数据库执行完全不必要的查询,首先按其ID提取Company记录,然后执行另一个查询来创建该关系(2个查询,而不是1个查询...)

因此,您可以将所需的关系字段(例如,从ajax请求中传递给您的快速应用程序(假设这是您正在使用的内容:):]]

const {companyId,goalId,objective,description} = req.body;

然后执行查询:

db.query(`insert into company_goal_relation (company_id,goal_id , objective, description,createdAt,updatedAt) values (?,?,?,?,NOW(),NOW()) `,{
        replacements:[companyId,goalId,objective,description]
    })

请注意,“ db”是您一开始就设置的序列化对象,您需要从config / setup文件中导入它。

当然,这不是执行此操作的“正常”续集方法,但我认为没有理由不应该这样做,然后结束了故事:D

发布评论

评论列表 (0)

  1. 暂无评论