我在“字段列表”中收到“未知”列错误尝试创建实例db.Partner.create
时。
代码正在运行,但不知何故它停止了(在我输入用户模型和一些中间件之后)
这是我收到的错误:
C:\Users\User\code\project\node_modules\mysql\lib\protocol\Parser.js:79
throw err; // Rethrow non-MySQL errors
^
Error: ER_BAD_FIELD_ERROR: Unknown column 'false' in 'field list'
at Query.Sequence._packetToError (C:\Users\User\code\project\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Query.ErrorPacket (C:\Users\User\code\project\node_modules\mysql\lib\protocol\sequences\Query.js:77:18)
at Protocol._parsePacket (C:\Users\User\code\project\node_modules\mysql\lib\protocol\Protocol.js:280:23)
at Parser.write (C:\Users\User\code\project\node_modules\mysql\lib\protocol\Parser.js:75:12)
at Protocol.write (C:\Users\User\code\project\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Users\User\code\project\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (C:\Users\User\code\project\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Connection.query (C:\Users\User\code\project\node_modules\mysql\lib\Connection.js:208:25)
at Query.module.exports.Query.run (C:\Users\User\code\project\node_modules\sequelize\lib\dialects\mysql\query.js:26:17)
at ConnectorManager.execQueueItem (C:\Users\User\code\project\node_modules\sequelize\lib\dialects\mysql\connector-manager.js:368:21)
at C:\Users\User\code\project\node_modules\sequelize\lib\dialects\mysql\connector-manager.js:326:25
at C:\Users\User\code\project\node_modules\generic-pool\lib\generic-pool.js:278:11
at ConnectorManager.connect (C:\Users\User\code\project\node_modules\sequelize\lib\dialects\mysql\connector-manager.js:295:5)
at Object.create (C:\Users\User\code\project\node_modules\sequelize\lib\dialects\mysql\connector-manager.js:125:19)
at createResource (C:\Users\User\code\project\node_modules\generic-pool\lib\generic-pool.js:258:13)
at dispense (C:\Users\User\code\project\node_modules\generic-pool\lib\generic-pool.js:250:9)
这是我的控制器:
var db = require('../models');
module.exports.addPartner = function(req_title, req_img){
var partner = db.Partner.create({
title: req_title,
img: req_img
});
}
我的模特:
"use strict";
module.exports = function(sequelize, DataTypes) {
var Partner = sequelize.define('Partner', {
id: {
type: DataTypes.INTEGER(12),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING,
allowNull: false
},
img: {
type: DataTypes.STRING,
allowNull: false,
set: function(val) {
return this.setDataValue('img', val.toLowerCase());
}
}
}, {
tableName: 'partner',
createdAt: 'created_at',
updatedAt: false
});
return Partner;
};
模型文件夹中的index.js:
var fs = require('fs');
var path = require('path');
var config = require('../../config/mysql');
var Sequelize = require('sequelize');
var lodash = require('lodash');
var db = {};
// mysql connect
var sequelize = new Sequelize(
config.database,
config.user,
config.password,
{
host: config.host,
port: config.port,
logging: false,
dialect: 'mysql'
}
);
fs.readdirSync(__dirname)
.filter(file => (file.indexOf('.') !== 0 && file !== 'index.js'))
.forEach(file => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model
});
Object.keys(db).forEach(modelName => {
if (db[modelName].options.hasOwnProperty('associate')) {
db[modelName].options.associate(db)
}
});
module.exports = lodash.extend({
sequelize,
Sequelize
}, db);