You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sequelize-Typescript严格模式下模型属性与数据库列名映射设置方法

解决方法

你需要使用sequelize-typescript提供的@Column装饰器,通过field属性指定数据库中的实际列名,具体修改如下:

  1. 先导入Column装饰器
  2. 在模型类中为categoryId字段添加@Column配置,指定field: 'category_id'

修改后的完整代码示例:

import { Optional } from 'sequelize';
import { Table, Model, Column } from 'sequelize-typescript'; // 新增Column导入

interface PersonAttributes {
  id: number;
  categoryId: string;
  name: string;
}

interface PersonCreationAttributes extends Optional<PersonAttributes, 'id'> {}

@Table
class Person extends Model<PersonAttributes, PersonCreationAttributes> {
  // 为categoryId指定数据库列名
  @Column({ field: 'category_id' })
  categoryId!: string;

  // 其他字段按需添加Column配置(若需自定义规则)
  @Column
  id!: number;

  @Column
  name!: string;
}

这样配置后,模型中的categoryId属性会映射到数据库的category_id列,同时保留严格模式的类型约束逻辑。

内容的提问来源于stack exchange,提问作者juliano.net

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 17:25:26