QueryFailedError:“price”列包含空值
我创建了一个简单的表:
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"
@Entity()
export class Test {
@PrimaryGeneratedColumn()
public id!: number
@Column({ nullable: false })
public name!: string
@Column({ nullable: false, type: "float" })
public price!: number
}
我生成迁移并运行它。当我在数据库中没有数据并运行服务器时它就成功了。但是当我在数据库中添加1行并再次运行它时,会出现以下错误:
QueryFailedError: the column «price» contain null values
数据库肯定有包含所有数据的行。我尝试了很多案例,但没有一个是正确的。
有人对此有所了解吗?
回答如下:我有一个类似的问题,我在this thread的底部报告了它。
您可能在ORM配置上有synchronize: true
。因此,每次运行应用程序时,Typeorm都会尝试创建表,如果有数据,则会抛出这种误导性错误。
来自here:
synchronize - 指示是否应在每次启动应用程序时自动创建数据库架构。请注意此选项,不要在生产中使用它 - 否则您可能会丢失生产数据。此选项在调试和开发期间非常有用。作为替代方案,您可以使用CLI并运行schema:sync命令。请注意,对于MongoDB数据库,它不会创建模式,因为MongoDB是无模式的。相反,它只是通过创建索引来同步。