Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an enum array in a TypeOrm entity?

I am doing a lot of research and experiment to use an enum array in my entity.

import { Program } from '../../program/entities/program.entity'
import {
  Column,
  Entity,
  JoinTable,
  ManyToMany,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm'
import { Exercise } from '../../exercise/entities/exercise.entity'
import { WeekDays } from '../types/week-days.enum'

@Entity()
export class Workout {
  // [...]

  @Column({
    type: 'enum',
    enum: WeekDays,
    default: [],
    array: true,
  })
  scheduledDays?: WeekDays[]

  constructor(partial: Partial<Workout> | {} = {}) {
    Object.assign(this, partial)
  }
}

Unfortunately it gives me this error.

QueryFailedError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'array ('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sund' at line 1

How can I solve this? I can't get what's wrong on my decorator options.

like image 271
A Mehmeto Avatar asked Jun 17 '26 11:06

A Mehmeto


1 Answers

leaving this here for anyone visiting this post for the same issue as me

   export enum StringEnum {
        ADMIN = "a",
        EDITOR = "e",
        MODERATOR = "m",
        GHOST = "g"
    }
    
    @Column({
        type: "enum",
        enum: StringEnum,
        array: true,
        default: [StringEnum.ADMIN]
    })
    stringEnums: StringEnum[];
like image 109
Samuel Akhaze Avatar answered Jun 20 '26 01:06

Samuel Akhaze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!