Home > Mobile >  Syntax error when adding an array in the entity - Node.js, TypeORM, MySQL
Syntax error when adding an array in the entity - Node.js, TypeORM, MySQL

Time:10-16

I'm using Node.js and TypeORM with MySQL but the error shows MariaDB I don't know why. I'm trying to use array in User entity and this must be the error

ERROR:

    QueryFailedError: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
    code: 'ER_PARSE_ERROR',
    errno: 1064,
    sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version 
for the right syntax to use near '' at line 1",
    sqlState: '42000',
    index: 0,
    sql: 'ALTER TABLE `user` ADD `favorites` text NOT NULL DEFAULT '
  },
  code: 'ER_PARSE_ERROR',
  errno: 1064,
  sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1",
  sqlState: '42000',
  index: 0,
  sql: 'ALTER TABLE `user` ADD `favorites` text NOT NULL DEFAULT '
}

The columns causing the error:

  @Column({ type: "simple-array", default: [] })
  favorites: string[];

  @Column({ type: "simple-array", default: [] })
  cart: string[];

CodePudding user response:

You can't have default value for column type text in mysql. Remove the default parameter in the column options and the query should work.

If you still want to have any default value set, you can write a native mysql trigger or use typeorm @AfterInsert hook to set the value of columns cart or favorites just after they're inserted in the database.

  • Related