存储库 - 插入方法

时间:2021-03-28 13:05:35

标签: javascript typescript typeorm

我正在使用 typeorm https://typeorm.io 并且我试图在 mysql 数据库的 users 表中插入一条新记录。

我有这个实体。


export interface UserEntity {
  id: number;
  email: string;
  password: string;
  confirmedSignUp: boolean;
  createdAt: string;
}

export const User = new EntitySchema<UserEntity>({
  name: "users",
  columns: {
    id: {
      type: "integer",
      primary: true,
      generated: true
    },
    email: {
      type: "varchar",
      unique: true,
      length: 150,
      nullable: false
    },
    password: {
      type: "varchar",
      length: 80,
      nullable: false,
      
    },
    confirmedSignUp: {
      type: "boolean",
      default: false
    },
    createdAt: {
      type: "timestamp",
      default: () => 'CURRENT_TIMESTAMP'
    }
  }
})

如您所见,mysql 需要字段 passwordemail,因为它们有 nullable: false。但是当我尝试使用 insert 方法插入新记录时,passwordemail 字段是可选的。正如文档所说,插入方法使用 QueryDeepPartialEntity 转换其可选形式的实体中的所有键。

http://typeorm-doc.exceptionfound.com/classes/repository.html#insert

当我想插入新记录时,有人可以解释我的这种行为吗?。不应该是不可为空的字段(在我的例子中是密码和电子邮件)?

谢谢!

0 个答案:

没有答案