首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在实体( Node.js、TypeORM、MySQL )中添加数组时出现语法错误

问在实体( Node.js、TypeORM、MySQL )中添加数组时出现语法错误
EN

Stack Overflow用户
提问于 2022-10-14 17:23:18
回答 1查看 51关注 0票数 0

我将Node.js和TypeORM与MySQL一起使用,但是错误显示了MariaDB,我不知道为什么。我试图在用户实体中使用数组,这一定是错误

误差

代码语言:javascript
复制
    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 '
}

导致错误的列:

代码语言:javascript
复制
  @Column({ type: "simple-array", default: [] })
  favorites: string[];

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-15 18:49:44

在mysql中,列类型text不能有默认值。删除列选项中的default参数,查询就会正常工作。

如果仍然希望设置任何默认值,则可以编写本机mysql触发器,或者在插入数据库后使用typeorm @AfterInsert钩子来设置列cart或favorites的值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74072753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档