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

Node.js+Sequelize ORM报错:users.username属性数据类型未识别

错误原因与修复方案

你遇到的Error: Unrecognized datatype for attribute "users.username"是因为模型定义里错误使用了dialectTypes字段来指定数据类型,Sequelize要求用type来定义字段的基础数据类型。

错误点分析

在你的用户模型中,每个字段的配置都用了dialectTypes: DataTypes.STRING,但这个属性是用于针对不同数据库方言(比如MySQL、PostgreSQL)自定义特殊数据类型的,不是用来设置常规基础类型的。Sequelize无法识别这个配置,所以抛出了数据类型未识别的错误。

修复后的模型代码

把所有字段的dialectTypes替换成type即可:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class users extends Model {
    static associate(models) {
      // define association here
    }
  }
  users.init({
    username: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    name: {
      type: DataTypes.STRING,
    },
    coverPic: {
      type: DataTypes.STRING,
    },
    profilePic: {
      type: DataTypes.STRING,
    },
    city: {
      type: DataTypes.STRING,
    },
    website: {
      type: DataTypes.STRING,
    }
  }, {
    sequelize,
    modelName: 'users',
  });
  return users;
};

额外说明

如果确实需要针对不同数据库设置不同的类型,才需要用到dialectTypes,比如:

someField: {
  type: DataTypes.STRING,
  dialectTypes: {
    mysql: 'VARCHAR(255)',
    postgres: 'TEXT'
  }
}

但你的场景里只需要基础的STRING类型,直接用type就足够了。

内容的提问来源于stack exchange,提问作者VijayRathod20

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 09:47:16