I have two models that are associated via hasMany on the source Model. It took a while as it was my first time using Sequelize, but I have been able to create a new user and get the list of users in the database using the include parameter. But when I try to use include an update, it does update the source Model, but not the information on the associated model
Model 1
import Sequelize from "sequelize";
import database from "../index";
import PlayerSkill from "./playerSkill";
const Player = database.define(
"player",
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
name: {
type: Sequelize.STRING(200),
allowNull: false,
},
position: {
type: Sequelize.STRING(200),
},
},
{
timestamps: false,
}
);
Player.hasMany(PlayerSkill, {
// through: "players_skills",
as: "playerSkills",
timestamps: false,
foreignKey: false,
// attributes: { exclude: ["playerSkillId"] },
});
// Player.hasMany(PlayerSkill);
// PlayerSkill.belongsTo(Player);
export default Player;
Model 2
import Sequelize from "sequelize";
import database from "../index";
const PlayerSkill = database.define(
"playerSkill",
{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true,
},
skill: {
type: Sequelize.STRING(200),
},
value: {
type: Sequelize.INTEGER,
},
},
{
timestamps: false,
}
);
export default PlayerSkill;
Create Route works fine
import Player from "../../db/model/player";
import PlayerSkill from "../../db/model/playerSkill";
export default async (req, res) => {
const { name, position, playerSkills } = req.body;
try {
const player = await Player.create(
{
name,
position,
playerSkills,
},
{
include: "playerSkills",
}
);
console.log(`Player created and saved!`);
res.json(player);
} catch (error) {
console.log(error.stack);
}
};
import Player from "../../db/model/player";
export default async (req, res) => {
const requestedId = req.params.id;
try {
const { name, position, playerSkills } = req.body;
const player = await Player.update(
{
name,
position,
playerSkills,
},
{
include: "playerSkills",
where: { id: requestedId },
}
);
res.json(player);
} catch (error) {
console.log(` This is the error ${error}`);
}
};
Update will only update the Player table
import Player from "../../db/model/player";
export default async (req, res) => {
const requestedId = req.params.id;
try {
const { name, position, playerSkills } = req.body;
const player = await Player.update(
{
name,
position,
playerSkills,
},
{
include: "playerSkills",
where: { id: requestedId },
}
);
res.json(player);
} catch (error) {
console.log(` This is the error ${error}`);
}
};
The model's update
method does not support include
option, so you either need to use generated helper methods in the main model to add/change/delete associated items or use direct methods of the associated model to do that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.