gpt4 book ai didi

node.js - SequelizeDatabaseError : column \"functionCode\" does not exist, 当它发生时

转载 作者:行者123 更新时间:2023-12-03 22:26:47 25 4
gpt4 key购买 nike

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

我禁用了时间戳列并在 CLI 上执行了 sequelize 有问题的 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中表对应的schema为:
       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)

Then there is standard NodeJS code (./models/index) that reads the model definition files to import the models. Edit - This is the only code I didn't provide but it is almost same as given here: https://sequelize.readthedocs.io/en/1.7.0/articles/express/

The query is executed in query.js file using following function:

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

最佳答案

表字段的名称使用小写或下划线,而不是驼峰式

关于node.js - SequelizeDatabaseError : column \"functionCode\" does not exist, 当它发生时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com