typeorm: QueryFailedError: column contains null values [SOLVED]

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

When setting the typeOrmConfig options to synchronize: true instead of synchronize: false, I get to see this error popping up:

QueryFailedError: column “account” contains null values

Does it has anything to do with the column count? I have multiple tables and entity classes and the only table that has 7 columns, is nagging about this issue. Below, I will paste down all the necessary code for you to review. Please fix this bug.

export const typeOrmConfig: TypeOrmModuleOptions = {
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    username: 'postgres',
    password: 'postgres',
    database: 'omega',
    entities: [__dirname + '/../**/*.entity.ts', __dirname + '/../**/*.entity.js'],
    synchronize: true
}
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, Unique } from "typeorm";

@Entity()
@Unique( [ 'account' ] )
export class HDWallet extends BaseEntity {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    mnemonic: string;

    @Column()
    seed: string;

    @Column()
    publicKey: string;

    @Column()
    privateKey: string;

    @Column()
    chainCode: string;

    @Column()
    account: number;

}

Error message:

[Nest] 10292   - 07/29/2019, 4:37 PM   [ExceptionHandler] column "account" contains null values +1ms
QueryFailedError: column "account" contains null values
    at new QueryFailedError (/Users/omega/Development/crypto-wallet-api/node_modules/typeorm/error/QueryFailedError.js:11:28)
    at Query.callback (/Users/omega/Development/crypto-wallet-api/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:176:38)
    at Query.handleError (/Users/omega/Development/crypto-wallet-api/node_modules/pg/lib/query.js:142:17)
    at Connection.connectedErrorMessageHandler (/Users/omega/Development/crypto-wallet-api/node_modules/pg/lib/client.js:211:17)
    at Connection.emit (events.js:200:13)
    at Socket.<anonymous> (/Users/omega/Development/crypto-wallet-api/node_modules/pg/lib/connection.js:126:12)
    at Socket.emit (events.js:200:13)
    at addChunk (_stream_readable.js:294:12)
    at readableAddChunk (_stream_readable.js:275:11)
    at Socket.Readable.push (_stream_readable.js:210:10)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 7
  • Comments: 29 (2 by maintainers)

Most upvoted comments

@Tolga-FNT Have you tried completely clearing out your destination folder for tsc? I noticed in my project that there where left over entities from when i changed file names ect, these entities where producing a error similar to the one you described.

We’ve cleared our build folder and all other tmp files yet we still receive this error. Anybody have any further recommendations?

try setting synchronize to false

Adding synchronize: false doesn’t “fix” anything, it just prevents TypeORM from making the database schema match the configured entities. Obviously in production it should be set to false because you wouldn’t want any data to be lost, but whatever problems you are encountering need to be fixed another way first, otherwise you may end up with undefined behavior.

The original problem described in this issue was about null values being stored in columns that were missing nullable: true. If you have a separate problem it’s probably best to open a new issue or ask around on the community Slack.

The default value for “nullable” on typeorm columns is false so you can’t have null values in your columns. So I’d guess the error message just means that you have a HDWallet instance with a null value for account.

Also check that postgres does allow unique constraints with null values if you want to have null values.

Actually, that what @SamuelColacchia suggested did fix the issue. Your answer is in another direction my friend, because I know what I am doing.

@Tolga-FNT Have you tried completely clearing out your destination folder for tsc? I noticed in my project that there where left over entities from when i changed file names ect, these entities where producing a error similar to the one you described.

sorry but where is this “destination folder for tsc” Thanks in advance

It is the directory listed in compilerOptions -> outDir property of tsconfig.json file.

YEs, but then you need to write migrations, but in my practice, during development it is faster and easy to use sync: true

Im having the same annoying problem with my app .i just set synchronize : false .works fine now

I often faced this issue when developing, you should just delete dist folder and restart application. It’s working well.

for me i make a table for manytomany relation so typeorm try first create a table from regualr

   @ManyToMany(type => Skills, skill => skill.users)
  @JoinTable()

then again from entitiy

@Entity('users_skills_skills',{
    synchronize:false
})
export class SkillUser  extends BaseEntity {

so just adding synchronize:false to the entity like above fix it

Same issue here, had to change synchronize to false, after restarting the application due to file changes it would throw "[Nest] 101380 - 02/05/2020, 21:41:26 [ExceptionHandler] column “userId” contains null values +1ms

QueryFailedError: column “userId” contains null values" Error

Checking DB would tell otherwise, strange bug.

@Tolga-FNT Have you tried completely clearing out your destination folder for tsc? I noticed in my project that there where left over entities from when i changed file names ect, these entities where producing a error similar to the one you described.

sorry but where is this “destination folder for tsc” Thanks in advance

besides setting up the column as nullable, you can also setup a default value! 😃 Which makes a lot of sense:

@Column({default: `x`})
test?: string;

Running in to this issue too, with a ManyToOne relationship resetting join column values to null on every restart.

I want to keep the advantages of the synchronize functionality, and would prefer not to have to track config exceptions in individual entities. Can confirm removing the dist folder and restarting doesn’t resolve. Changing the order of entity creation (as suggested by @andresogando) also doesn’t resolve.

Many to One:

  // PayPeriod.entity.ts
  @ManyToOne(
    () => PayrollSchedule,
    (payroll_schedule) => payroll_schedule.pay_periods,
  )
  @JoinColumn({ name: 'payroll_schedule_id' })
  payroll_schedule: PayrollSchedule;

OneToMany:

  //payrollSchedule.entity.ts
  @OneToMany(() => PayPeriod, (pay_period) => pay_period.payroll_schedule)
  pay_periods: PayPeriod[];

TypeORM logging reveals:

[1] query: ALTER TABLE "pay_period" DROP CONSTRAINT "FK_e0a993b13e76c5ff3fcd9851864"
[1] query: ALTER TABLE "pay_period" DROP COLUMN "payroll_schedule_id"
[1] query: ALTER TABLE "pay_period" ADD "payroll_schedule_id" integer
[1] query: ALTER TABLE "pay_period" ADD CONSTRAINT "FK_e0a993b13e76c5ff3fcd9851864" FOREIGN KEY ("payroll_schedule_id") REFERENCES "payroll_schedule"("id") ON DELETE NO ACTION ON UPDATE NO ACTION

I interpret this as the column being dropped and reinitiated, but have no clue as to why. I’ve used MTO/OTM joins all over my codebase, but can’t understand this anomaly.

@pleerock Can you please take a look on this issue, I have seen 2 issue posts that mention the exact same problem from 2018! This issue is very annoying and I am losing time here. It seems that the issue occurs on the last column every time.

Edit: this issue occurs only when modifying the entity data by adding 1 or more columns after you run the project. Is there a way to clear the cache/(typeorm)files to have a clean start?