QueryFailedError: the column "price" contain null values - TypeORM - PostgreSQL

Viewed 32170

I've created a simple table:

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
}

I generate the migration and run it also. When I have no data in the database and I run the server it succeed. But when I add 1 row in the database and I run it again it appears the following error:

QueryFailedError: the column «price» contain null values

The databse definetely has the row with all the data. I tried a lot of cases and none of them was correct.

Has anybody some idea for it?

7 Answers

I had a similar issue, and I reported it at the bottom of this thread.

You probably have synchronize: true on your ORM configuration. Because of this, every time you run your app Typeorm tries to create the tables. If you have data in your DB, it throws that misleading error.

From here:

synchronize - Indicates if database schema should be auto created on every application launch. Be careful with this option and don't use this in production - otherwise you can lose production data. This option is useful during debug and development. As an alternative to it, you can use CLI and run schema:sync command. Note that for MongoDB database it does not create schema, because MongoDB is schemaless. Instead, it syncs just by creating indices.

The Problem

This line of code right here is the troublemaker:

@Column({ nullable: false, type: "float" })
public price!: number

You have set nullable: false, which means you never want this column to be null.

Why is this giving you trouble?

Because you already have rows in your database that don't have an entry for this column.

Remember:

Prior to this migration, the column didn't exist. But now you're running a migration to add a new column.

But TypeORM needs a way to handle the existing rows...

So, if you were TypeORM, what value would you assign by default if the column didn't exist? Probably NULL. Which is exactly what it does. And that's why the error.

How do you fix this issue?

There are two ways here.

Very easy way

Set a default value in the ColumnOptions

@Column({ nullable: false, type: "float", default: 0.0 })
public price!: number

DANGER: The nuclear option

Delete all data from the table, then re-run the migration.

In Postgres PSQL:

DELETE FROM test;

I had this entity

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {

@PrimaryGeneratedColumn()
id!: number;

@Column('text')
firstName!: string;

}

and I was getting the error "column "firstName" contains null values." So I added {nullable: true} as @pleerock suggested here https://github.com/typeorm/typeorm/issues/845 and it worked.

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User {

@PrimaryGeneratedColumn()
id!: number;

@Column('text', {nullable: true})
firstName: string;

}

Set "synchronize": false in ormconfig.json will fix this issue:

{
   ....
   "synchronize": false,
   ....
}

What if you delete the nullable declaration or what if you set it to true ...

Then you can do the nullable check using validation decorators from class-validator.

I had the same issue. It looks like when you add a new column without a default value, the default value of the column will be null and typeorm will not allow that if you don't explicitly specify that it's ok to be null

For me it worked after I had added in the @Column({nullable: true}). This tells typeorm that it's ok to have null in the column.

Or as @nathanl93 suggested, you could give a default value @Column({default: 'aDefaultValue'}) and the new column will be added with the specified default value every time if no other value will be specified.

You will need to make sure that you have the parameter "syncronize": true where you have configured your connection to the db. For me it was the ormconfig.json file.

I had the same issue. You can set a default value and nullable: true as said before. Also, what helped me was to delete the .dist folder in order to recreate it on the build with the new changes.

Related