Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 为什么我的sequelize查询在急切地加载一个与另一个具有belongsTo/hasMany的模型关联时会出现ER\u BAD\u FIELD\u错误? 续集版本:_Mysql_Sql_Node.js_Database Design_Sequelize.js - Fatal编程技术网

Mysql 为什么我的sequelize查询在急切地加载一个与另一个具有belongsTo/hasMany的模型关联时会出现ER\u BAD\u FIELD\u错误? 续集版本:

Mysql 为什么我的sequelize查询在急切地加载一个与另一个具有belongsTo/hasMany的模型关联时会出现ER\u BAD\u FIELD\u错误? 续集版本:,mysql,sql,node.js,database-design,sequelize.js,Mysql,Sql,Node.js,Database Design,Sequelize.js,4.37.10 模型定义: LocalChapters.js NationalChapters.js 已运行查询: 预期结果: 收到错误: DatabaseError[SequelizeDatabaseError]:“字段列表”中的未知列“localchapters.NationalChapterdNationalChapter” at Query.formatError(//node_modules/sequelize/lib/dialogs/mysql/Query.js:247:16) 在Q

4.37.10

模型定义: LocalChapters.js NationalChapters.js 已运行查询: 预期结果: 收到错误:
DatabaseError[SequelizeDatabaseError]:“字段列表”中的未知列“localchapters.NationalChapterdNationalChapter”
at Query.formatError(//node_modules/sequelize/lib/dialogs/mysql/Query.js:247:16)
在Query.handler[as onResult](//node_modules/sequelize/lib/dialogs/mysql/Query.js:68:23)
在Query.Command.execute(//node_modules/mysql2/lib/commands/Command.js:30:12)
位于Connection.handlePacket(//node_modules/mysql2/lib/Connection.js:502:28)
在PacketParser.onPacket(//node_modules/mysql2/lib/connection.js:81:16)
在PacketParser.executeStart(//node_modules/mysql2/lib/packet_parser.js:77:14)
在插座上。(//node_modules/mysql2/lib/connection.js:89:29)
位于Socket.emit(events.js:210:5)
在addChunk(_stream_readable.js:308:12)
在readableAddChunk(_stream_readable.js:289:11)
在Socket.Readable.push(_stream_Readable.js:223:10)
在TCP.onStreamRead(internal/stream_base_commons.js:182:23){
名称:“SequelizeDatabaseError”,
父项:错误:“字段列表”中的未知列“localchapters.NationalChapterdNationalChapter”
在Packet.asError(//node_modules/mysql2/lib/packets/Packet.js:716:13)
在Query.Command.execute(//node_modules/mysql2/lib/commands/Command.js:28:22)
位于Connection.handlePacket(//node_modules/mysql2/lib/Connection.js:502:28)
在PacketParser.onPacket(//node_modules/mysql2/lib/connection.js:81:16)
在PacketParser.executeStart(//node_modules/mysql2/lib/packet_parser.js:77:14)
在套接字上。//node_modules/mysql2/lib/connection.js:89:29)
位于Socket.emit(events.js:210:5)
在addChunk(_stream_readable.js:308:12)
在readableAddChunk(_stream_readable.js:289:11)
在Socket.Readable.push(_stream_Readable.js:223:10)
在TCP.onStreamRead(internal/stream_base_commons.js:182:23){
代码:“ER_BAD_FIELD_ERROR”,
errno:1054,
sqlState:'42S22',
sqlMessage:“字段列表”中的未知列“localchapters.NationalChapterdNationalChapter”,
sql:'选择'nationalchapters`.'idNationalChapter`,
`nationalchapters`.`NationalName`,
`localchaptes`.`idLocalChapter`作为`localchaptes.idLocalChapter`,
`localchapters`.`LocalName`作为`localchapters.LocalName`,
`localchapters`.`NationalChapter\u id`作为`localchapters.NationalChapter\u id`,
`localchapters`.`NationalChapterdNationalChapter`作为`localchapters.NationalChapterdNationalChapter`
从“国家章节”改为“国家章节”
左外连接'localchapters'为'localchapters'
关于'nationalchapters'。'idNationalChapter`='localchapters`.'NationalChapterdNationalChapter`;'
},
原始:错误:“字段列表”中的未知列“localchapters.NationalChapterdNationalChapter”
在Packet.asError(//node_modules/mysql2/lib/packets/Packet.js:716:13)
在Query.Command.execute(//node_modules/mysql2/lib/commands/Command.js:28:22)
位于Connection.handlePacket(//node_modules/mysql2/lib/Connection.js:502:28)
在PacketParser.onPacket(//node_modules/mysql2/lib/connection.js:81:16)
在PacketParser.executeStart(//node_modules/mysql2/lib/packet_parser.js:77:14)
在套接字上。//node_modules/mysql2/lib/connection.js:89:29)
位于Socket.emit(events.js:210:5)
在addChunk(_stream_readable.js:308:12)
在readableAddChunk(_stream_readable.js:289:11)
在Socket.Readable.push(_stream_Readable.js:223:10)
在TCP.onStreamRead(internal/stream_base_commons.js:182:23){
代码:“ER_BAD_FIELD_ERROR”,
errno:1054,
sqlState:'42S22',
sqlMessage:“字段列表”中的未知列“localchapters.NationalChapterdNationalChapter”,
sql:'选择'nationalchapters`.'idNationalChapter`,
`nationalchapters`.`NationalName`,
`localchaptes`.`idLocalChapter`作为`localchaptes.idLocalChapter`,
`localchapters`.`LocalName`作为`localchapters.LocalName`,
`localchapters`.`NationalChapter\u id`作为`localchapters.NationalChapter\u id`,
`localchapters`.`NationalChapterdNationalChapter`作为`localchapters.NationalChapterdNationalChapter`
从“国家章节”改为“国家章节”
左外连接'localchapters'为'localchapters'
关于'nationalchapters'。'idNationalChapter`='localchapters`.'NationalChapterdNationalChapter`;'
},
sql:'选择'nationalchapters`.'idNationalChapter`,
`nationalchapters`.`NationalName`,
`localchaptes`.`idLocalChapter`作为`localchaptes.idLocalChapter`,
`localchapters`.`LocalName`作为`localchapters.LocalName`,
`localchapters`.`NationalChapter\u id`作为`localchapters.NationalChapter\u id`,
`localchapters`.`NationalChapterdNationalChapter`作为`localchapters.NationalChapterdNationalChapter`
从“国家章节”改为“国家章节”
左外连接'localchapters'为'localchapters'
关于'nationalchapters'。'idNationalChapter`='localchapters`.'NationalChapterdNationalChapter`;'
}
我降落的地方: 这似乎是一个基本的归属,它/它有许多关系,正在生成损坏的SQL,但我一辈子都无法找出我的错误所在。。。有什么想法吗


编辑:
-修复了“查询运行”部分中型号名称的打字错误

已解决

当使用自定义外键在模型之间创建一对多关联时,显然您必须在中指定外键配置,这两个
都属于
有许多关联(而不仅仅是其中一个关联)
const Database = require('./database');
const Sequelize = require('sequelize');
const NationalChapters = require('./nationalchapters');

const LocalChapters = Database
.getInstance()
.define('localchapters', {
    idLocalChapter: {
        type: Sequelize.INTEGER(11),
        primaryKey: true,
        allowNull: false,
        autoIncrement: true,
        unique: true
    },
    LocalName: {
        type: Sequelize.STRING(45),
        allowNull: false
    }
}, {
    timestamps: false
});

LocalChapters.belongsTo(NationalChapters, { foreignKey: 'NationalChapter_id', onDelete: 'CASCADE' });
NationalChapters.hasMany(LocalChapters);

module.exports = LocalChapters;
const Database = require('./database');
const Sequelize = require('sequelize');

const NationalChapters = Database
.getInstance()
.define('nationalchapters', {
    idNationalChapter: {
        type: Sequelize.INTEGER(11),
        primaryKey: true,
        allowNull: false,
        autoIncrement: true,
        unique: true
    },
    NationalName: {
        type: Sequelize.STRING(45),
        allowNull: false,
        unique: true
    }
}, {
    timestamps: false
});

module.exports = NationalChapters;
const result = await NationalChapters.findAll({
    include: [ LocalChapters ]
});
[
  {
    idNationalChapter: 21,
    NationalName: 'Some National Chapter Name',
    localchapters: [
      {
        idLocalChapter: 15,
        LocalName: 'Some Local Chapter Name'
      },
      ...
    ]
  }
  ...
]
DatabaseError [SequelizeDatabaseError]: Unknown column 'localchapters.nationalchapterIdNationalChapter' in 'field list'
    at Query.formatError (/<file path>/node_modules/sequelize/lib/dialects/mysql/query.js:247:16)
    at Query.handler [as onResult] (/<file path>/node_modules/sequelize/lib/dialects/mysql/query.js:68:23)
    at Query.Command.execute (/<file path>/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/<file path>/node_modules/mysql2/lib/connection.js:502:28)
    at PacketParser.onPacket (/<file path>/node_modules/mysql2/lib/connection.js:81:16)
    at PacketParser.executeStart (/<file path>/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/<file path>/node_modules/mysql2/lib/connection.js:89:29)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:308:12)
    at readableAddChunk (_stream_readable.js:289:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
  name: 'SequelizeDatabaseError',
  parent: Error: Unknown column 'localchapters.nationalchapterIdNationalChapter' in 'field list'
      at Packet.asError (/<file path>/node_modules/mysql2/lib/packets/packet.js:716:13)
      at Query.Command.execute (/<file path>/node_modules/mysql2/lib/commands/command.js:28:22)
      at Connection.handlePacket (/<file path>/node_modules/mysql2/lib/connection.js:502:28)
      at PacketParser.onPacket (/<file path>/node_modules/mysql2/lib/connection.js:81:16)
      at PacketParser.executeStart (/<file path>/node_modules/mysql2/lib/packet_parser.js:77:14)
      at Socket.<anonymous> (/<file path>/node_modules/mysql2/lib/connection.js:89:29)
      at Socket.emit (events.js:210:5)
      at addChunk (_stream_readable.js:308:12)
      at readableAddChunk (_stream_readable.js:289:11)
      at Socket.Readable.push (_stream_readable.js:223:10)
      at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
    code: 'ER_BAD_FIELD_ERROR',
    errno: 1054,
    sqlState: '42S22',
    sqlMessage: "Unknown column 'localchapters.nationalchapterIdNationalChapter' in 'field list'",
    sql: 'SELECT `nationalchapters`.`idNationalChapter`, 
                `nationalchapters`.`NationalName`, 
                `localchapters`.`idLocalChapter` AS `localchapters.idLocalChapter`, 
                `localchapters`.`LocalName` AS `localchapters.LocalName`, 
                `localchapters`.`NationalChapter_id` AS `localchapters.NationalChapter_id`, 
                `localchapters`.`nationalchapterIdNationalChapter` AS `localchapters.nationalchapterIdNationalChapter` 
            FROM `nationalchapters` AS `nationalchapters` 
            LEFT OUTER JOIN `localchapters` AS `localchapters` 
            ON `nationalchapters`.`idNationalChapter` = `localchapters`.`nationalchapterIdNationalChapter`;'
  },
  original: Error: Unknown column 'localchapters.nationalchapterIdNationalChapter' in 'field list'
      at Packet.asError (/<file path>/node_modules/mysql2/lib/packets/packet.js:716:13)
      at Query.Command.execute (/<file path>/node_modules/mysql2/lib/commands/command.js:28:22)
      at Connection.handlePacket (/<file path>/node_modules/mysql2/lib/connection.js:502:28)
      at PacketParser.onPacket (/<file path>/node_modules/mysql2/lib/connection.js:81:16)
      at PacketParser.executeStart (/<file path>/node_modules/mysql2/lib/packet_parser.js:77:14)
      at Socket.<anonymous> (/<file path>/node_modules/mysql2/lib/connection.js:89:29)
      at Socket.emit (events.js:210:5)
      at addChunk (_stream_readable.js:308:12)
      at readableAddChunk (_stream_readable.js:289:11)
      at Socket.Readable.push (_stream_readable.js:223:10)
      at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
    code: 'ER_BAD_FIELD_ERROR',
    errno: 1054,
    sqlState: '42S22',
    sqlMessage: "Unknown column 'localchapters.nationalchapterIdNationalChapter' in 'field list'",
    sql: 'SELECT `nationalchapters`.`idNationalChapter`, 
                `nationalchapters`.`NationalName`, 
                `localchapters`.`idLocalChapter` AS `localchapters.idLocalChapter`, 
                `localchapters`.`LocalName` AS `localchapters.LocalName`, 
                `localchapters`.`NationalChapter_id` AS `localchapters.NationalChapter_id`, 
                `localchapters`.`nationalchapterIdNationalChapter` AS `localchapters.nationalchapterIdNationalChapter` 
            FROM `nationalchapters` AS `nationalchapters` 
            LEFT OUTER JOIN `localchapters` AS `localchapters` 
            ON `nationalchapters`.`idNationalChapter` = `localchapters`.`nationalchapterIdNationalChapter`;'
  },
  sql: 'SELECT `nationalchapters`.`idNationalChapter`, 
                `nationalchapters`.`NationalName`, 
                `localchapters`.`idLocalChapter` AS `localchapters.idLocalChapter`, 
                `localchapters`.`LocalName` AS `localchapters.LocalName`, 
                `localchapters`.`NationalChapter_id` AS `localchapters.NationalChapter_id`, 
                `localchapters`.`nationalchapterIdNationalChapter` AS `localchapters.nationalchapterIdNationalChapter` 
            FROM `nationalchapters` AS `nationalchapters` 
            LEFT OUTER JOIN `localchapters` AS `localchapters` 
            ON `nationalchapters`.`idNationalChapter` = `localchapters`.`nationalchapterIdNationalChapter`;'
}
LocalChapters.belongsTo(NationalChapters, { foreignKey: 'NationalChapter_id', onDelete: 'CASCADE' });
NationalChapters.hasMany(LocalChapters, { foreignKey: 'NationalChapter_id', onDelete: 'CASCADE' });