TypeORM evokes error in findOne query - QueryFailedError: ER_NO_DEFAULT_FOR_FIELD: Field 'type' doesn't have a default value

Viewed 2080

I am using MySQL database backed by TypeORM and Nodejs. I have an entity that records holidays. now the insertion works pretty well, but the problem is findOne query does not work.

Here is the entity:

import {
  Column,
  CreateDateColumn,
  Entity,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
  VersionColumn,
} from "typeorm";

@Entity()
export class Holiday {
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @Column()
  type: string;

  @Column({ nullable: true, default: null })
  weekDay?: string;

  @Column({ nullable: true, default: null })
  date?: Date;

  @Column({ default: null })
  remark?: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @VersionColumn()
  version: number;
}

This is the query I'm executing to findOne record:

const holiday = await getConnection()
        .manager.getRepository(Holiday)
        .findOne(id); //id of the record that needs to find.

The Error I encounter:

QueryFailedError: ER_NO_DEFAULT_FOR_FIELD: Field 'type' doesn't have a default value
code: 'ER_NO_DEFAULT_FOR_FIELD',
errno: 1364,
sqlMessage: "Field 'type' doesn't have a default value",
sqlState: 'HY000',
index: 0,
sql: "INSERT INTO `holiday`(`id`, `type`, `weekDay`, `date`, `remark`, `createdAt`, `updatedAt`, `version`) VALUES ('b8b93d11-2346-4388-bf87-738b61c41118', DEFAULT, DEFAULT, '2021-08-10 06:53:58.213', DEFAULT, DEFAULT, DEFAULT, 1)"
1 Answers

I think you have issue at following

@Column({ nullable: true, default: null })
date?: Date;

You can not put Date type to null

Try this:

@Column()
date?: Date;
Related