해결된 질문
작성
·
629
0
D:\Study\Server\nodejs\nodejsLib\login\node_modules\sequelize\lib\model.js:230
throw new Error(`A column called 'id' was added to the attributes of '${this.tableName}' but not marked with 'primaryKey: true'`);
^
Error: A column called 'id' was added to the attributes of 'users' but not marked with 'primaryKey: true'
at Function._addDefaultAttributes (D:\Study\Server\nodejs\nodejsLib\login\node_modules\sequelize\lib\model.js:230:15)
at Function.init (D:\Study\Server\nodejs\nodejsLib\login\node_modules\sequelize\lib\model.js:1052:10)
at Sequelize.define (D:\Study\Server\nodejs\nodejsLib\login\node_modules\sequelize\lib\sequelize.js:428:11)
at module.exports (D:\Study\Server\nodejs\nodejsLib\login\models\user.js:2:22)
at Object.<anonymous> (D:\Study\Server\nodejs\nodejsLib\login\models\index.js:15:28)
at Module._compile (internal/modules/cjs/loader.js:971:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
at Module.load (internal/modules/cjs/loader.js:822:32)
at Function.Module._load (internal/modules/cjs/loader.js:730:14)
at Module.require (internal/modules/cjs/loader.js:864:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Study\Server\nodejs\nodejsLib\login\app.js:11:21)
at Module._compile (internal/modules/cjs/loader.js:971:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
at Module.load (internal/modules/cjs/loader.js:822:32)
at Function.Module._load (internal/modules/cjs/loader.js:730:14)
at Module.require (internal/modules/cjs/loader.js:864:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (D:\Study\Server\nodejs\nodejsLib\login\bin\www:7:11)
at Module._compile (internal/modules/cjs/loader.js:971:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
at Module.load (internal/modules/cjs/loader.js:822:32)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! login@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the login@0.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\dnatu\AppData\Roaming\npm-cache\_logs\2019-11-05T13_48_03_653Z-debug.log
테이블 코드는 선생님 코드와 같지는 않고 필드명과 테이블 명만 제가 원하는 대로 살짝 수정하긴 했습니다만 이것이 영향을 미치는 것 같지는 않습니다! index.js는 기본으로 생성되는 페이지 그대로 사용했습니다.
user.js
module.exports = (sequelize, DataTypes) => {
return sequelize.define('user', {
name:{
type: DataTypes.STRING(5),
allowNULL: false,
unique: false,
},
id: {
type: DataTypes.STRING(30),
allowNULL: false,
unique: true,
},
passwd: {
type: DataTypes.STRING(20),
allowNULL: false,
unique: false,
},
age: {
type: DataTypes.INTEGER(2).UNSIGNED,
allowNULL: false,
unique: false,
},
phone_num: {
type: DataTypes.INTEGER(12).UNSIGNED,
allowNULL: false,
unique: true,
},
created_at: {
type: DataTypes.DATE,
allowNULL: false,
defautlValue: sequelize.literal('now()'),
},
store_name:{
type: DataTypes.STRING(20),
allowNULL: false,
unique: true,
}
}, {
timestamp: false,
underscored: true,
});
};
// user 테이블 정리
// 이름(5), 아이디(30), 비밀번호(20), 나이(2), 휴대폰 번호(12), 계정생성일(DATE->default(now)), 마켓 이름(20)
iteam.js
module.exports = (sequelize, DataTypes) => {
return sequelize.define('iteam', {
iteam_name: {
type: DataTypes.STRING(30),
allowNULL: false,
unique: false,
},
item_price: {
type: DataTypes.INTEGER(6).UNSIGNED,
allowNULL: false,
unique: false,
},
item_count: {
type: DataTypes.INTEGER(6).UNSIGNED,
allowNULL: false,
unique: false,
},
item_description: {
type: DataTypes.TEXT,
allowNULL: true,
unique: false,
},
created_at:{
type: DataTypes.DATE,
allowNULL: false,
defautlValue: sequelize.literal('now()'),
},
},{
timestamp: false,
underscored: true,
});
};
// store 테이블 정리
// 마켓 이름, 물건명, 가격, 물품 개 수, 물품 설명, 등록일자
index.js
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
//테이블 추가
db.User = require('./user')(sequelize, Sequelize);
db.Iteam = require('./iteam')(sequelize, Sequelize);
//관계 설정
db.User.hasMany(db.Iteam, { foreignKey: 'store_name', sourceKey: 'id' });
db.Iteam.belongsTo(db.User, { foreignKey: 'store_name', targetKey: 'id'});
module.exports = db;