typeORM: "message": "Data type \"Object\" in \"..." is not supported by \"postgres\" database."
Asked Answered
S

2

34

Given the following entity definition:

@Entity()
export class User extends BaseEntity {
  @Column({ nullable: true })
  name!: string | null;
  
  @Column()
  age!: number;
}

The following error appears:

typeORM:   "message": "Data type \"Object\" in \"User.name" is not supported by \"postgres\" database."

...

name: 'DataTypeNotSupportedError',
  message:
   'Data type "Object" in "User.name" is not supported by "postgres" database.' }

When looking at the build, I see that the metadata that's emitted by TS addresses it as object:

__decorate([
    typeorm_1.Column({ nullable: true }),
    __metadata("design:type", Object)
], User.prototype, "name", void 0);

What am I doing wrong?

Scientist answered 14/10, 2020 at 9:54 Comment(1)
I found an answer to this problem hereNekton
M
61

You need to provide a data type explicitly.

@Column({ type: 'varchar', nullable: true })
name: string | null;
Mccloud answered 14/5, 2021 at 14:13 Comment(2)
This needs to be marked as the answer. Thanks a lot!Grimsley
Thank you, you saved a lot of my time, I was confused because I wasn't getting any error but "Connection is not established with mysql database"Tabber
S
13

The issue stems from this part right here:

@Column({ nullable: true })
name!: string | null;

When creating a union type, the reflected type will be Object. An easy way to overcome it will be to do something like:

@Column({ nullable: true })
name?: string;
Scientist answered 14/10, 2020 at 9:54 Comment(2)
Does the DB return undefined if the name is null? Is that something that TypeORM ensures to be consistent? If it returns null instead, this could seak in some errors. Doing name === undefined will be fine for the compiler, but if the actual value is null during runtime, the check will not guard from name being null.Malek
If name is null in the database, it will come back as null in the entity. So the correct type is string | null, not string | undefined.Shcherbakov

© 2022 - 2024 — McMap. All rights reserved.