gpt4 book ai didi

node.js - 续写 TypeScript : How to use `createdAt` in scope's `where` condition if it is not as part of model's attributes?

转载 作者:行者123 更新时间:2023-12-03 22:33:43 26 4
gpt4 key购买 nike

我尝试将 Sequelize 与 TypeScript 一起使用。我从 official document TypeScript section 复制代码像这样:


// 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;
}
但是,当我初始化 User 表时,我想定义一个默认范围,例如 updatedAt提交必须满足一些条件:
  User.init(userTableAttributes,
{
tableName: "user",
sequelize,
paranoid: true,
defaultScope: {
where: {
updatedAt: {
[Op.eq]: null
}
}
}
}
);
但是这段代码是错误的,好像是因为 updatedAt UserAttributes 中未定义的字段.
那么我该如何解决这个问题呢?我只想出一个办法:定义 updatedAt字段在 UserAttributes ?但这没有任何意义,因为我认为 UserAttributes只包括业务属性,对吗?

最佳答案

我发现我是否添加了 paranoid ,它会自动添加 deletedAt is NULL查询字符串中的条件

关于node.js - 续写 TypeScript : How to use `createdAt` in scope's `where` condition if it is not as part of model's attributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63989537/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com