작성
·
181
0
기존의 hashtag모델을 Comment모델로 바꾸는 시도를 했는데요,
Object.assign(options, _.omit(source.options, ['hooks']));
^
TypeError: Cannot assign to read only property 'name' of function 'class Comment extends Sequelize.Model {
static init(sequelize) {
return super.init(
{
c...<omitted>...
}'
at Function.assign (<anonymous>)
at Function.hasMany (C:\Users\DAAE\OneDrive - Chonnam National University\2021-1\Project\collusic\implement\collusic\backend\node_modules\sequelize\lib\associations\mixin.js:27:12)
at Function.associate (C:\Users\DAAE\OneDrive - Chonnam National University\2021-1\Project\collusic\implement\collusic\backend\models\user.js:49:13)
at Object.<anonymous> (C:\Users\DAAE\OneDrive - Chonnam National University\2021-1\Project\collusic\implement\collusic\backend\models\index.js:25:6)
이러한 에러가 나옵니다. post와 user 테이블도 바꿨었는데 hashtag모델을 바꾸니 문제가 생기네요..
기존의 databases는 mysql에서 drop 한 상태에서 실습중입니다!
//models/user.js
const Sequelize = require("sequelize");
//db 테이블과 sequelize 연동을 위한 코드
module.exports = class User extends Sequelize.Model {
static init(sequelize) {
return super.init(
{
email: {
type: Sequelize.STRING(40),
allowNull: true,
unique: true,
},
nick: {
type: Sequelize.STRING(15),
allowNull: false,
},
password: {
type: Sequelize.STRING(100),
allowNull: true,
},
provider: {
//로그인 제공자
type: Sequelize.STRING(10),
allowNull: false,
defaultValue: "local",
},
introduce: {
type: Sequelize.TEXT,
allowNull: true,
},
imagePath: {
type: Sequelize.STRING(100),
allowNull: true,
},
},
{
sequelize,
timestamps: true, //생성일 수정일 삭제일이 기록
underscored: false,
modelName: "User",
tableName: "users",
paranoid: true,
charset: "utf8",
collate: "utf8_general_ci", //한글 지원
}
);
}
static associate(db) {
db.User.hasMany(db.Post, db.Comment, {
foreignKey: "uid",
targetKey: "id",
});
}
};
//models/post.js
const Sequelize = require("sequelize");
module.exports = class Post extends Sequelize.Model {
static init(sequelize) {
return super.init(
{
title: {
//id 생략. -> sequlize에서는 id가 생략될 수 있음.
type: Sequelize.STRING(140),
allowNull: false,
},
description: {
type: Sequelize.TEXT,
allowNull: true,
},
audioFile: {
type: Sequelize.STRING(140),
allowNull: false,
},
state: {
type: Sequelize.BOOLEAN,
allowNull: false,
},
field_free: {
type: Sequelize.BOOLEAN,
allowNull: false,
},
lyrics_text: {
type: Sequelize.TEXT,
allowNull: true,
},
genre: {
type: Sequelize.STRING(140),
allowNull: true,
},
mood: {
type: Sequelize.STRING(140),
allowNull: true,
},
music_field: {
type: Sequelize.BOOLEAN,
allowNull: true,
},
lyrics_field: {
type: Sequelize.BOOLEAN,
allowNull: true,
},
instrument_field: {
type: Sequelize.BOOLEAN,
allowNull: true,
},
},
{
sequelize,
timestamps: true,
underscored: false,
modelName: "Post",
tableName: "posts",
paranoid: false, //deleted at false -> 게시글 삭제시 완전 삭제
charset: "utf8mb4", //이모티콘
collate: "utf8mb4_general_ci",
}
);
}
static associate(db) {
db.Post.belongsTo(db.User, { foreignKey: "uid", sourceKey: "id" });
db.Post.hasMany(db.Comment, { foreignKey: "pid", targetKey: "id" });
}
};
//models/comment.js
const Sequelize = require("sequelize");
module.exports = class Comment extends Sequelize.Model {
static init(sequelize) {
return super.init(
{
c_description: {
type: Sequelize.TEXT,
allowNull: false,
},
c_audioFile: {
type: Sequelize.STRING(140),
allowNull: false,
},
c_lyrics_text: {
type: Sequelize.TEXT,
allowNull: true,
},
selected_status: {
type: Sequelize.BOOLEAN,
allowNull: false,
},
},
{
sequelize,
timestamps: true,
underscored: false,
modelName: "Comment",
tableName: "comment",
paranoid: false,
charset: "utf8mb4",
collate: "utf8mb4_general_ci",
}
);
}
static associate(db) {
db.Comment.belongsTo(db.Post, { foreignKey: "pid", sourceKey: "id" });
db.Comment.belongsTo(db.User, { foreignKey: "uid", sourceKey: "id" });
}
};
//requestid 외래키
//uid 외래키
//models/index.js
const Sequelize = require("sequelize");
const env = process.env.NODE_ENV || "development"; //config.json의 development 가져오기
const config = require("../config/config")[env]; //config.json의 development 가져오기
const User = require("./user");
const Post = require("./post");
const Comment = require("./comment");
const db = {};
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
config
);
db.sequelize = sequelize;
db.User = User;
db.Post = Post;
db.Comment = Comment;
//사람과 게시글은 1:1관계, 게시글과 해시태그는 1:N 관계
User.init(sequelize);
Post.init(sequelize);
Comment.init(sequelize);
User.associate(db);
Post.associate(db);
Comment.associate(db);
module.exports = db;
답변 1
0
db.User.hasMany(db.Post, db.Comment, {
foreignKey: "uid",
targetKey: "id",
});
여기가 이상하네요.
로 바꾸니 잘되었습니다! 답변이 빠르셔서 굉장히 감동입니다ㅜ 감사합니다!