// This file makes all join table relationships
const db = process.env.DB_DATABASE;
const dbHost = process.env.DB_HOST;
const dbUser = process.env.DB_USER;
const dbPassword = process.env.DB_PASS;
const Sequelize = require('sequelize');
const sequelize = new Sequelize(db, dbUser, dbPassword, {
  dialect: 'mariadb',
  host: dbHost
});

// Any variable that starts with a capital letter is a model
const User = require('./users.js')(sequelize, Sequelize);
const Bookmark = require('./bookmarks.js')(sequelize, Sequelize);
const BookmarkUsers = require('./bookmarkUsers')(sequelize, Sequelize);
const Photo = require('./photos.js')(sequelize, Sequelize);
const Comment = require('./comments.js')(sequelize, Sequelize);
const Like = require('./likes.js')(sequelize, Sequelize);

// BookmarkUsers join table:
User.belongsToMany(Bookmark, {
  through: 'bookmark_users',
  foreignKey: 'user_id'
});

Bookmark.belongsToMany(User, {
  through: 'bookmark_users',
  foreignKey: 'bookmark_id'
});

//Photos and Users
User.hasMany(Photo, {
  foreignKey: 'poster_id'
});

Photo.belongsTo(User, {
  foreignKey: 'poster_id'
});

// Comments
User.belongsToMany(Photo, {
  through: 'comments',
  foreignKey: 'user_id'
});

Photo.belongsToMany(User, {
  through: 'comments',
  foreignKey: 'photo_id'
});

// Likes
User.belongsToMany(Photo, {
  through: 'likes',
  foreignKey: 'user_id'
});

Photo.belongsToMany(User, {
  through: 'likes',
  foreignKey: 'photo_id'
});

//Create missing tables, if any
// sequelize.sync({force: true});
sequelize.sync();


exports.User = User;
exports.Bookmark = Bookmark;
exports.BookmarkUsers = BookmarkUsers;
exports.Photo = Photo;
exports.Comment = Comment;
exports.Like = Like;
@@ -1123,58 +1123,32 @@
 nts%0A
-User.belongsToMany(Photo, %7B%0A  through: 'c
+Photo.hasMany(C
 omment
-s',
+, %7B
 %0A  f
@@ -1151,36 +1151,37 @@
 %0A  foreignKey: '
-user
+photo
 _id'%0A%7D);%0A%0APhoto.
@@ -1166,37 +1166,39 @@
 'photo_id'%0A%7D);%0A%0A
-Photo
+Comment
 .belongsToMany(U
@@ -1195,43 +1195,17 @@
 gsTo
-Many(User, %7B%0A  through: 'comments',
+(Photo, %7B
 %0A  f

Change comments table to avoid Sequelize bug