node.js - Sequelize TypeScript:如果它不是模型属性的一部分,如何在范围的`where`条件中使用`createdAt`?
问题描述
我尝试将 Sequelize 与 TypeScript 一起使用。我从官方文档 TypeScript 部分复制代码,如下所示:
// These are all the attributes in the User model
interface UserAttributes {
id: number;
name: string;
preferredName: string | null;
}
interface UserCreationAttributes extends Optional<UserAttributes, "id"> {}
class User extends Model<UserAttributes, UserCreationAttributes>
implements UserAttributes {
public id!: number;
public name!: string;
public preferredName!: string | null;
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
但是,当我初始化用户表时,我想定义一个默认范围,比如字段updatedAt
必须满足一些条件:
User.init(userTableAttributes,
{
tableName: "user",
sequelize,
paranoid: true,
defaultScope: {
where: {
updatedAt: {
[Op.eq]: null
}
}
}
}
);
但是这段代码是错误的,似乎是因为updatedAt
未定义的字段UserAttributes
。
那么我该如何解决这个问题呢?我只想出一种方法:定义updatedAt
字段UserAttributes
?但这没有任何意义,因为我认为UserAttributes
唯一的包括业务属性,对吧?
解决方案
我发现如果我添加了,它会在查询字符串中paranoid
自动添加条件deletedAt is NULL
推荐阅读
- javascript - Firebase Firestore 不保存数组数据
- python - 函数 Output_Assignment_Variable
- javascript - 如何为 getData() 响应提供嵌套的 JSON 数据?
- android - 按钮颜色没有改变。Android Studio
- bash - 用于递归遍历每个子目录并在每个级别上创建一个新目录的 Bash 脚本
- python - 将文本文件转换为 csv 文件,但分隔符似乎没有做任何事情
- java - 即使已指定 th:object,Java Thymeleaf @ModelAttribute 控制器值为 null
- python - 大型 JSON 文件更新缓慢
- haskell - 如何在需要以不同方式处理第一个元素的递归调用中创建列表?
- firebase - Flatlist 不显示来自 firebase 的数据