Версия Sequelize:
4.37.10
Определения модели:
LocalChapters. js
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;
NationalChapters. js
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`;'
}
Где я приземлился:
Это похоже на базовые c отношения "принадлежит / имеет много", которые порождают нарушенные SQL, но я не могу на всю жизнь понять, где я ошибаюсь ... Есть идеи?
Правки: - Исправлена опечатка для названия модели в разделе "Выполнение запроса"