SequelizeDatabaseError:列\“ functionCode \”不存在

时间:2019-06-22 03:23:57

标签: node.js sequelize.js postgresql-11

我正在编写一个简单的NodeJS代码,以使用sequelize从postgres中读取/写入。我的代码适用于一个表,但是当测试另一个不相关的表时,类似的代码也会中断。从错误堆栈跟踪可以看出,我还发现抛出的错误令人困惑。

我禁用了timestamp列,并在CLI上执行了SQL,后继出现了问题。

首先,这是/tfnMetadata.js

中的模型
'use strict';

/**
 * Model definition: See http://docs.sequelizejs.com/manual/models-definition.html
 *  See DataTypes: http://docs.sequelizejs.com/manual/data-types.html
 * @param
 * @returns {*} records.
 */
module.exports = (sequelize, DataTypes) => {
  const tfnMetadata = sequelize.define('tfn_metadata', {
    did: {
      type: DataTypes.STRING, primaryKey: true, allowNull: false,
    },
    dnis: {
      type: DataTypes.STRING,
    },
    functionCode: {
      type: DataTypes.STRING,
    },
    country: {
      type: DataTypes.STRING,
    },
    language: {
      type: DataTypes.STRING,
    },
    coBrand: {
      type: DataTypes.BOOLEAN,
    },
    collections: {
      type: DataTypes.BOOLEAN,
    },
    smallBusiness: {
      type: DataTypes.BOOLEAN,
    },
    welcomeMessageCode: {
      type: DataTypes.STRING,
    },
    welcomeMessage: {
      type: DataTypes.TEXT,
    },
    referenceKey: {
      type: DataTypes.STRING,
    },
    partner: {
      type: DataTypes.STRING,
    },
  }, { timestamps: false, tableName: 'tfn_metadata' });
  tfnMetadata.sync({ alter: true });
  return tfnMetadata;
};

psql中表的对应架构为:

       Column       |          Type          | Nullable | 
--------------------+------------------------+----------+
 did                | character varying(15)  | not null |
 dnis               | character varying(15)  |          |
 functioncode       | character varying(20)  |          |
 country            | character varying(25)  |          |
 language           | character varying(20)  |          |
 cobrand            | boolean                |          |
 collections        | boolean                |          |
 smallbusiness      | boolean                |          |
 welcomemessagecode | text                   |          |
 welcomemessage     | character varying(200) |          |
 referencekey       | character varying(50)  |          |
 partner            | character varying(50)

然后有标准的NodeJS代码(./models/index)读取模型定义文件以导入模型。编辑-这是我没有提供的唯一代码,但与此处给出的代码几乎相同:https://sequelize.readthedocs.io/en/1.7.0/articles/express/

使用以下函数在 query.js 文件中执行查询:

const models = require('./models/index');

const getByPk = async (didNumber) => {
  try {
    const record = await models.tfnMetadata.findByPk(didNumber.toString());
    logger.info('Query.getByPk() Returned the record from db.', record);
    return record.dataValues;
  } catch (err) {
    logger.error('Query.getByPk() failed:', err);
    throw err;
  }
};

以上代码在client.js中的调用方式为-


const query = require('../../src/clients/pg/query');
const tfnData = await query.getByPk(did);


但是失败--

Query.getByPk() Request to get record for did=111 undefined
Query.getByPk() failed: {
  "name": "SequelizeDatabaseError",
  "parent": {
    "name": "error",
    "length": 184,
    "severity": "ERROR",
    "code": "42703",
    "hint": "Perhaps you meant to reference the column \"tfn_metadata.functioncode\".",
    "position": "23",
    "file": "parse_relation.c",
    "line": "3294",
    "routine": "errorMissingColumn",
    "sql": "SELECT \"did\", \"dnis\", \"functionCode\", \"country\", \"language\", \"coBrand\", \"collections\", \"smallBusiness\", \"welcomeMessageCode\", \"welcomeMessage\", \"referenceKey\", \"partner\" FROM \"tfn_metadata\" AS \"tfn_metadata\" WHERE \"tfn_metadata\".\"did\" = '111';"
  }
error: End - Error:
error: {
    "errorMessage": "column \"functionCode\" does not exist",
    "errorType": "SequelizeDatabaseError",
    "stackTrace": [
        "Query.formatError (/Users/.../node_modules/sequelize/lib/dialects/postgres/query.js:354:16)",
        "query.catch.err (/Users/.../node_modules/sequelize/lib/dialects/postgres/query.js:71:18)",
        "tryCatcher (/Users/.../node_modules/bluebird/js/release/util.js:16:23)",
        "Promise._settlePromiseFromHandler (/Users/.../node_modules/bluebird/js/release/promise.js:512:31)",

错误代码表明未定义列或缺少列functionCode,提示正在询问我是否要引用functionCode

1 个答案:

答案 0 :(得分:1)

使用小写或下划线作为表字段的名称,而不是camelCase