如何更新行子项添加外键

问题描述

我有这两个模型:

//Change Image to Correct Orientation When displaying to PictureBox
public static RotateFlipType Rotate ( Image bmp ) {
    const int OrientationId = 0x0112;
    PropertyItem pi = bmp.PropertyItems.Select( x => x )
                                .FirstOrDefault( x => x.Id == OrientationId );
    if( pi == null )
        return RotateFlipType.RotateNoneFlipNone;

    byte o = pi.Value[ 0 ];

    //Orientations
    if( o == 2 ) //TopRight
        return RotateFlipType.RotateNoneFlipX;
    if( o == 3 ) //Bottomright
        return RotateFlipType.RotateNoneFlipXY;
    if( o == 4 ) //BottomLeft
        return RotateFlipType.RotateNoneFlipY;
    if( o == 5 ) //LeftTop
        return RotateFlipType.Rotate90FlipX;
    if( o == 6 ) //RightTop
        return RotateFlipType.Rotate90FlipNone;
    if( o == 7 ) //RightBottom
        return RotateFlipType.Rotate90FlipY;
    if( o == 8 ) //LeftBottom
        return RotateFlipType.Rotate90FlipXY;

    return RotateFlipType.RotateNoneFlipNone; //TopLeft (what the image looks by default) [or] UnkNown
}
module.exports = (sequelize,Sequelize) => {
    const Image = sequelize.define("Image",{
        url: {
            type: Sequelize.STRING,validate: {
                notEmpty: true,}
        },public_id: {
            type: Sequelize.STRING,alt: {
            type: Sequelize.STRING,},not_link: {
            type: Sequelize.BOOLEAN,defaultValue: false
        },});

    Image.associate = models => {
        Image.belongsToMany(models.Project,{
            as: 'projects',through: "project_image"
        });
        Image.belongsToMany(models.Page,{
            through: "page_image"
        });
    };

    return Image;
};

我想用新图像更新项目。我尝试了很多东西,但没有任何效果,而且我在文档中找不到任何内容

这是我最后一次尝试:

module.exports = (sequelize,Sequelize) => {
    const Project = sequelize.define("Project",{
        title_fr: {
            type: Sequelize.STRING,title_en: {
            type: Sequelize.STRING,}
        }
    });

    Project.associate = models => {
        Project.belongsTo(models.Image,{
            as: 'cover',foreignKey: {
                name: 'cover_img_id',allowNull: false
            }
        });
        Project.belongsToMany(models.Image,{
            as: 'medias',through: "project_image"
        });
    };

    return Project;
};

还有我的路线:

exports.updateOneProject = (req,res) => {
    const id = req.params.id;
    const projectObject = {
        ...req.body
    }
    if(req.files){
        let files= [];
        req.files.forEach(function(file){
            console.log(file)
            files.push(
                {
                    url: file.path,alt: file.alt,public_id:file.filename
                }
            )
        });
        projectObject.medias=files
    }
    Project.update(projectObject,{
        where: { id: id }
    })
        .then(num => {
            if (num == 1) {
                res.send({
                    message: "Project was updated successfully."
                });
            } else {
                res.send({
                    message: `Cannot update Partner with slug=${id}. Maybe Partner was not found or req.body is empty!`
                });
            }
        })
        .catch(err => {
            res.status(500).send({
                message: "Error updating Partner with slug=" + id
            });
        });
};

没有报错,所有变量都定义好了,请求返回200,但是图片没有存入数据库。我想要一个存储在数据库中的新图像,以及一个 project_image 中间表中的新关系。在创建项目时,没有问题,但无法更新。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)