through된 부분도 강의 12:10에 보면 테이블로 만들어져 있는데 저는 그게 없는데 원인을 뭘로 볼 수 있을까요?
(follow, like, posthashtag이런게 없습니다. 딱 js로 만든 다섯개만 테이블로 나와요)
User.js 코드
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define(
"User",
{
nickname: {
type: DataTypes.STRING(20),
allowNull: false,
},
userId: {
type: DataTypes.STRING(20),
allowNull: false,
unique: true,
},
password: {
type: DataTypes.STRING(100),
allowNull: false,
},
},
{
charset: "utf8",
collate: "utf8_general_ci", // can use Korean
}
);
User.associate = (db) => {
db.User.hasMany(db.Post, { as: "Posts" });
db.User.hasMany(db.Comment);
db.User.belongsToMany(db.Post, { through: "Like", as: "Liked" });
db.User.belongsToMany(db.User, { through: "Follow", as: "Followers" });
db.User.belongsToMany(db.User, { through: "Follow", as: "Followings" });
};
return User;
};
Post.js 코드
module.exports = (sequelize, DataTypes) => {
const Post = sequelize.define(
"Post",
{
content: {
type: DataTypes.TEXT,
allowNull: false,
},
},
{
charset: "utf8mb4",
collate: "utf8mb4_general_ci", // can use Korean
}
);
Post.associate = (db) => {
db.Post.belongsTo(db.User);
db.Post.belongsToMany(db.User, { through: "Like", as: "Likers" });
db.Post.belongsTo(db.Post, { as: "Retweet" });
db.Post.hasMany(db.Comment);
db.Post.hasMany(db.Image);
db.Post.belongsToMany(db.Hashtag, { through: "PostHashtag" });
};
return Post;
};
index.js 코드
const Sequelize = require("sequelize");
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
);
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.Comment = require("./comment")(sequelize, Sequelize);
db.Hashtag = require("./hashtag")(sequelize, Sequelize);
db.Image = require("./image")(sequelize, Sequelize);
db.Post = require("./post")(sequelize, Sequelize);
db.User = require("./user")(sequelize, Sequelize);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;