Sequelize-Typescript严格模式下模型属性与数据库列名映射设置方法
解决方法
你需要使用sequelize-typescript提供的@Column装饰器,通过field属性指定数据库中的实际列名,具体修改如下:
- 先导入
Column装饰器 - 在模型类中为
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
相关产品推荐
相关产品推荐

