Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.JS:Sequelize关联集无法还原paranoid上的记录_Node.js_Sequelize.js - Fatal编程技术网

Node.JS:Sequelize关联集无法还原paranoid上的记录

Node.JS:Sequelize关联集无法还原paranoid上的记录,node.js,sequelize.js,Node.js,Sequelize.js,我有两个模型users和tags,它们都通过另一个名为usersTags的模型关联,并且所有3个模型都有带有自定义时间戳的偏执设置。我知道关联模型将添加其他方法来处理所有关联模型的关联,因此我想为用户发出一个简单的setTags调用,文档显示,如果方法中的数组中不包含存储在数据库中的元素,则应将其删除,否则,应创建/恢复它 因此,我尝试恢复以前删除的标记,但由于某些原因,它失败了。模型定义如下: 用户 module.exports = function(sequelize, DataTypes)

我有两个模型
users
tags
,它们都通过另一个名为
usersTags
的模型关联,并且所有3个模型都有带有自定义时间戳的偏执设置。我知道关联模型将添加其他方法来处理所有关联模型的关联,因此我想为用户发出一个简单的
setTags
调用,文档显示,如果方法中的数组中不包含存储在数据库中的元素,则应将其删除,否则,应创建/恢复它

因此,我尝试恢复以前删除的
标记
,但由于某些原因,它失败了。模型定义如下:

用户

module.exports = function(sequelize, DataTypes) {
    const Users = sequelize.define("users", {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        username: {
            type: DataTypes.STRING(100),
            allowNull: false,
            validate: {
                len: {
                    args: [3, 100],
                    msg: "String length is not in this range"
                }
            }
        },
        password: {
            type: DataTypes.STRING(100),
            allowNull: false,
            field: "password_hash"
        }
    }, {
        tableName: "users",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true
    });

    Users.associate = function(models) {
        // Add this association to include tag records
        this.belongsToMany(models.tags, {
            through: {
                model: models.usersTags,
                unique: true
            },
            foreignKey: "users_id",
            constraints: false
        });
    };

    return Users;
};
module.exports = function(sequelize, DataTypes) {
    const Tags = sequelize.define("tags", {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(45),
            allowNull: false
        }
    }, {
        tableName: "tags",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true
    });

    Tags.associate = function(models) {
        this.belongsToMany(models.users, {
            through: {
                model: models.usersTags,
                unique: true
            },
            foreignKey: "tags_id",
            constraints: false
        });
    };

    return Tags;
};
module.exports = function(sequelize, DataTypes) {
    const UsersTags = sequelize.define("usersTags", {
        users_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            references: {
                model: "users",
                key: "id"
            }
        },
        tags_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            references: {
                model: "tags",
                key: "id"
            }
        }
    }, {
        tableName: "users_tags",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true,
        indexes: [
            {
                unique: true,
                fields: ["users_id", "tags_id"]
            }
        ]
    });

    return UsersTags;
};
标签

module.exports = function(sequelize, DataTypes) {
    const Users = sequelize.define("users", {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        username: {
            type: DataTypes.STRING(100),
            allowNull: false,
            validate: {
                len: {
                    args: [3, 100],
                    msg: "String length is not in this range"
                }
            }
        },
        password: {
            type: DataTypes.STRING(100),
            allowNull: false,
            field: "password_hash"
        }
    }, {
        tableName: "users",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true
    });

    Users.associate = function(models) {
        // Add this association to include tag records
        this.belongsToMany(models.tags, {
            through: {
                model: models.usersTags,
                unique: true
            },
            foreignKey: "users_id",
            constraints: false
        });
    };

    return Users;
};
module.exports = function(sequelize, DataTypes) {
    const Tags = sequelize.define("tags", {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(45),
            allowNull: false
        }
    }, {
        tableName: "tags",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true
    });

    Tags.associate = function(models) {
        this.belongsToMany(models.users, {
            through: {
                model: models.usersTags,
                unique: true
            },
            foreignKey: "tags_id",
            constraints: false
        });
    };

    return Tags;
};
module.exports = function(sequelize, DataTypes) {
    const UsersTags = sequelize.define("usersTags", {
        users_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            references: {
                model: "users",
                key: "id"
            }
        },
        tags_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            references: {
                model: "tags",
                key: "id"
            }
        }
    }, {
        tableName: "users_tags",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true,
        indexes: [
            {
                unique: true,
                fields: ["users_id", "tags_id"]
            }
        ]
    });

    return UsersTags;
};
usersTags

module.exports = function(sequelize, DataTypes) {
    const Users = sequelize.define("users", {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        username: {
            type: DataTypes.STRING(100),
            allowNull: false,
            validate: {
                len: {
                    args: [3, 100],
                    msg: "String length is not in this range"
                }
            }
        },
        password: {
            type: DataTypes.STRING(100),
            allowNull: false,
            field: "password_hash"
        }
    }, {
        tableName: "users",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true
    });

    Users.associate = function(models) {
        // Add this association to include tag records
        this.belongsToMany(models.tags, {
            through: {
                model: models.usersTags,
                unique: true
            },
            foreignKey: "users_id",
            constraints: false
        });
    };

    return Users;
};
module.exports = function(sequelize, DataTypes) {
    const Tags = sequelize.define("tags", {
        id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: DataTypes.STRING(45),
            allowNull: false
        }
    }, {
        tableName: "tags",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true
    });

    Tags.associate = function(models) {
        this.belongsToMany(models.users, {
            through: {
                model: models.usersTags,
                unique: true
            },
            foreignKey: "tags_id",
            constraints: false
        });
    };

    return Tags;
};
module.exports = function(sequelize, DataTypes) {
    const UsersTags = sequelize.define("usersTags", {
        users_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            references: {
                model: "users",
                key: "id"
            }
        },
        tags_id: {
            type: DataTypes.INTEGER(11),
            allowNull: false,
            primaryKey: true,
            references: {
                model: "tags",
                key: "id"
            }
        }
    }, {
        tableName: "users_tags",
        createdAt: "create_time",
        updatedAt: "update_time",
        deletedAt: "delete_time",
        paranoid: true,
        indexes: [
            {
                unique: true,
                fields: ["users_id", "tags_id"]
            }
        ]
    });

    return UsersTags;
};
测试

let _user;

models.users.findOne({where: {id: 100}})
    .then(user => {
        _user = user;

        return _user.setTags([1]);          // Successfully create association tag with id 1
    })
    .then(() => _user.setTags([]))   // Successfully remove all associated tags
    .then(() => _user.setTags([1])); // Should restore association tag with id 1 but fails
执行的查询

app:database Executing (default): SELECT `id`, `username`, `first_name`, `last_name`, `birthday`, `description`, `location`, `email`, `type`, `image_path` FROM `users` AS `users` WHERE ((`users`.`delete_time` > '2018-08-28 19:40:15' OR `users`.`delete_time` IS NULL) AND `users`.`id` = 100); +0ms
app:database Executing (default): SELECT `users_id`, `tags_id`, `create_time`, `update_time`, `delete_time` FROM `users_tags` AS `usersTags` WHERE ((`usersTags`.`delete_time` > '2018-08-28 19:40:15' OR `usersTags`.`delete_time` IS NULL) AND `usersTags`.`users_id` = 100); +6ms
app:database Executing (default): INSERT INTO `users_tags` (`users_id`,`tags_id`,`create_time`,`update_time`) VALUES (100,1,'2018-08-28 19:40:15','2018-08-28 19:40:15'); +7ms

由于某些原因,标记搜索查询无法检索包含删除时间集的标记,因此最后一次查询是
insert
而不是
update
,我知道解决方法是将
paranoid
设置为false,但我必须跟踪所有活动,我知道另一个解决方法是创建一个自定义模型方法来处理这个问题,但我仍然想知道是否有一种方法可以实现这一点,而不必创建一个额外的自定义方法

您的代码没有以正确的异步顺序进行,因此您的
\u用户
全局变量没有启动,我认为这是正确的顺序:

let _user;

models.users.findOne({where: {id: 100}})
.then(user => {
    _user = user;
    _user.setTags([]).then(()=>{
        _user.setTags([1])
    })
})

我忘了添加退货,原始代码确实有退货。。我为此修复编辑了我的问题