작성자 없음
작성자 정보가 삭제된 글입니다.
작성
·
147
·
수정됨
0
경매 코드를 작성한 후 동작을 시켰을 때 메인 화면에 아래 그림과 같은 에러가 발생 하였습니다
에러 발생 부분의 코드는 controllers/index.js 파일 부분인데 특별한 오류 점을 찾을 수가 없어 (작성한 파일들은 guithub 에 올려주신 파일들과 비교할 때 틀린 점을 발견하지 못함) 강사님께 문제 해결을 위해 질문 드립니다
해당 코드 부분
try {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1); //어제시간
const goods = await Good.findAll( {
where: { SoldId: null, createdAt: { [Op.gte] : yesterday} },
});
res.render ('main', {
title: 'NodeAuction',
goods,
});
에러 로그 내용:
Error
at Query.run (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\sequelize\lib\dialects\mysql\query.js:52:25) at D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\sequelize\lib\sequelize.js:315:28
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async MySQLQueryInterface.select (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\sequelize\lib\dialects\abstract\query-interface.js:407:12)
at async Good.findAll (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\sequelize\lib\model.js:1140:21)
at async exports.renderMain (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\controllers\index.js:7:19) {
name: 'SequelizeDatabaseError',
parent: Error: Unknown column 'Good.SoldId' in 'where clause'
at Packet.asError (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\packets\packet.js:728:17)
at Query.execute (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\commands\command.js:29:26)
at Connection.handlePacket (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\connection.js:481:34)
at PacketParser.onPacket (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\connection.js:97:12)
at PacketParser.executeStart (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\connection.js:104:25)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5) {
code: 'ER_BAD_FIELD_ERROR',
errno: 1054,
sqlState: '42S22',
sqlMessage: "Unknown column 'Good.SoldId' in 'where clause'",
sql: "SELECT `id`, `name`, `img`, `price`, `createdAt`, `updatedAt`, `deletedAt` FROM `goods` AS `Good` WHERE (`Good`.`deletedAt` IS NULL AND (`Good`.`SoldId` IS NULL AND `Good`.`createdAt` >= '2024-07-02 01:58:59'));",
parameters: undefined
},
original: Error: Unknown column 'Good.SoldId' in 'where clause'
at Packet.asError (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\packets\packet.js:728:17)
at Query.execute (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\commands\command.js:29:26)
at Connection.handlePacket (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\connection.js:481:34)
at PacketParser.onPacket (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\connection.js:97:12)
at PacketParser.executeStart (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (D:\Javascript_Work\node_works\node_js_3\skyc_work\node_auction\node_modules\mysql2\lib\connection.js:104:25)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5) {
code: 'ER_BAD_FIELD_ERROR',
errno: 1054,
sqlState: '42S22',
sqlMessage: "Unknown column 'Good.SoldId' in 'where clause'",
sql: "SELECT `id`, `name`, `img`, `price`, `createdAt`, `updatedAt`, `deletedAt` FROM `goods` AS `Good` WHERE (`Good`.`deletedAt` IS NULL AND (`Good`.`SoldId` IS NULL AND `Good`.`createdAt` >= '2024-07-02 01:58:59'));",
parameters: undefined
},
sql: "SELECT `id`, `name`, `img`, `price`, `createdAt`, `updatedAt`, `deletedAt` FROM `goods` AS `Good` WHERE (`Good`.`deletedAt` IS NULL AND (`Good`.`SoldId` IS NULL AND `Good`.`createdAt` >= '2024-07-02 01:58:59'));",
parameters: {}
}
답변 3
0
0
강사님,말씀 하신 데로 workbench에 이상하게 goods table에 SoldID 칼럼이 생성 되어 있지 않습니다. auctions table에는 GoodId, UserId 칼럼이 생성되어 있는데요..
참고로 models/index.js 파일 코드를 올립니다
const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');
const env = process.env.NODE_ENV || 'development';
const config = require('../config/config')[env];
const db = {};
const sequelize = new Sequelize(
config.database, config.username, config.password, config,
);
db.sequelize = sequelize;
//db.Sequelize = Sequelize;
const basename = path.basename(__filename); //현재 작업하는 file 이름 찾을려고
fs
.readdirSync(__dirname)
.filter( file => { // linux 에서는 앞에 . 가 붙으면 숨김파일임
return file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js';
})
.forEach( (file) => {
const model = require(path.join(__dirname, file));
console.log(file, model.name);
db[model.name] = model;
model.initiate(sequelize);
//model.associate(sequelize); <-- 이건 안됨 여기서, 왜? 모델 생성 다하고 서로간 연결 해야 하므로...
});
Object.keys(db).forEach(modelName => {
//console.log(db, modelName);
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
module.exports = db;
0
소문자로 시도했는데 해결 되진 않습니다 강사님
good.js 입니다
const { STRING } = require('sequelize');
const Sequelize = require('sequelize');
class Good extends Sequelize.Model {
static initiate(sequelize) {
Good.init({
name: {
type: Sequelize.STRING(40),
allowNull: false,
},
img : {
type: Sequelize.STRING(200),
allowNull: true,
},
price: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
}, {
sequelize,
timestamps: true, //createdAt, updatedAt
underscored: false,
modelName: 'Good',
tableName: 'goods',
paranoid: true, //deletedAt // soft delete
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
});
}
static associte(db) {
db.Good.belongsTo(db.User, {as: 'Owner' });
db.Good.belongsTo(db.User, {as: 'Sold' });
db.Good.hasMany(db.Auction);
}
};
module.exports = Good;
user.js 입니다
const { STRING } = require('sequelize');
const Sequelize = require('sequelize');
class User extends Sequelize.Model {
static initiate(sequelize) {
User.init( {
email: {
type: Sequelize.STRING(40),
allowNull: true,
unique: true,
},
nick : {
type: Sequelize.STRING(15),
allowNull: false,
},
password: {
type: Sequelize.STRING(100),
allowNull: true,
},
money: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
}
/*
provider: {
type:Sequelize.ENUM('local','kakao'),
allowNull: false,
defaultValue: 'local',
},
snsId: {
type: Sequelize.STRING(30),
allowNull: true,
}
*/
}, {
sequelize,
timestamps: true, //createdAt, updatedAt
underscored: false,
modelName: 'User',
tableName: 'users',
paranoid: true, //deletedAt // soft delete
charset: 'utf8mb4',
collate: 'utf8mb4_general_ci',
});
}
static associate(db) {
db.User.hasMany(db.Auction);
/*
db.User.belongsToMany(db.User, { // 팔로워
foreignKey: 'followingId',
as: 'Followers',
through: 'Follow'
})
db.User.belongsToMany(db.User, { // 팔로잉
foreignKey: 'followerId',
as: 'Followings',
through: 'Follow'
})
*/
}
}
module.exports = User;
associate 함수 호출하는 부분이 있나요? associate 부분이 호출이 안 되어 SoldId가 생성되지 않은 것 같습니다. 워크벤치로 테이블 보시면 더 명확할 것 같네요.
sequelize.sync({ force: true }) 로 수정해서 서버 재실행하면 테이블 제대로 생성될 겁니다(데이터는 전부 날아갑니다)