typeorm - TypeORM:插入新实体时发生错误:“id”列中的空值违反非空约束
问题描述
我正在使用 PostgreSQL 和 TypeORM。我的创建表是:
CREATE TABLE "my_table"("id" uuid NOT NULL, CONSTRAINT PK_my_table PRIMARY KEY (id));
我的实体类定义是:
import { Entity, Generated, PrimaryColumn } from 'typeorm';
@Entity('my_table')
export class MyTable {
@PrimaryColumn({ type: 'uuid' })
@Generated('uuid')
id!: string;
}
当我跑
entityManager.getRepository(MyTable).save({});
我收到错误消息:
(node:85) UnhandledPromiseRejectionWarning: QueryFailedError: null value in column "id" violates not-null constraint
所以似乎没有生成 id 。这很奇怪,因为当其他实体保存在应用程序中时,这是有效的。
可能有什么问题??我已经简化了测试这个问题的真实情况。
解决方案
我手动生成了 uuid,我的实体是:
@Entity('my_table')
export class MyTable {
@PrimaryColumn({ type: 'uuid' })
id!: string;
}
这是有效的。
推荐阅读
- ios - 在 iPad 和 iPhone 之间连接蓝牙
- c# - 如何在没有 Moq 的情况下在 C# 中实例化 FormFile 的实例?
- pyspark - Spark Dataframe 在 count() 上返回不一致的值
- tensorflow - Loss、accuracy、validation loss、Validation accuracy 有什么区别?
- qt - 通过 QSoftFilterProxyModel 的项目数
- python - Python kivy md 导航栏位置?
- javascript - 无法为未定义的变量赋值
- nltk - 从返回 None 的 nltk.metrics.score 中召回
- reactjs - 如何在特定条件下隐藏按钮?
- javascript - Netsuite 套件 + 将数据从 Get 传递到 Post