0

I've been stuck on this for a good hour, and unfortunately can't find anything relevant in the docs.

I'm unable to set values on fields that use types like enum or uuid. For instance:

export enum PhotoStatus {
  WORKING = 'WORKING',
  DONE = 'DONE'
}

export class Photo {
  @PrimaryGeneratedColumn()
  id: number:
  
  @Column()
  size: string;

  @Column({
    type: 'enum',
    enum: PhotoStatus,
    default: PhotoStatus.WORKING,
  })
  status: PhotoStatus;
}

But when I try to create a new record, like so:

  const photo = new Photo();
  photo.size = 'huge';
  photo.status = PhotoStatus.DONE;
  await connection.manager.save(photo);

It throws an error:

BadRequestException: ERROR: column "status" is of type content_status_enum but expression is of type character varying
  Hint: You will need to rewrite or cast the expression.

This happens for uuid columns too.

8
  • Maybe this can help: stackoverflow.com/a/45153609/460557 Commented Sep 3, 2020 at 20:41
  • @JorgeCampos Unfortunately not. I don't want a text field, I want an actual enum in the DB. Commented Sep 3, 2020 at 20:47
  • I mean his first suggestion in the answer. marking it as int and using the IsEnum decorator? Commented Sep 3, 2020 at 21:08
  • 1
    I'd rather avoid using int if possible, as values like 7 or 3 in the database don't really indicate what the value means. This is why I'd prefer to use an enum type. Commented Sep 3, 2020 at 21:16
  • Problem is right now there is no way around it since typeorm doesn't support postgresql enums. Either you change the database or your code to workaround it. As it is mentioned in this github issue Commented Sep 3, 2020 at 21:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.