I am using Sequelize for my database interfacing and I have run into a problem.
I am building a project revolving around restaurants and in the database I am trying to add restaurant specific menus. So there is one restaurant associated to many menus. Here is my code for creating all my associations.
const Company = require("./Company");
const Employee = require("./Employee");
const Restaurant = require("./Restaurant");
const CompMenu = require("./Comp-menu");
const RestMenu = require("./Rest-menu");
Company.hasMany(Employee, { foreignKey: 'company_id' });
Employee.belongsTo(Company, { foreignKey: 'company_id' });
Company.hasMany(Restaurant, { foreignKey: 'company_id' });
Restaurant.belongsTo(Company, { foreignKey: 'company_id' });
Company.hasMany(CompMenu, { foreignKey: 'company_id' });
CompMenu.belongsTo(Company, { foreignKey: 'company_id' });
// Restaurant - Restaurant Menu Associations Here
Restaurant.hasMany(RestMenu, { foreignKey: 'restaurant_id'});
RestMenu.belongsTo(Restaurant, { foreignKey: 'restaurant_id'});
Company.hasMany(RestMenu, { foreignKey: 'company_id' });
RestMenu.belongsTo(Company, { foreignKey: 'company_id' });
Restaurant.belongsToMany(Employee, { through: "Restaurant_Employees"});
Employee.belongsToMany(Restaurant, { through: "Restaurant_Employees"});
module.exports={
Company,
Employee,
Restaurant,
CompMenu,
RestMenu,
};
But the type of the foreignKey being generated for the restaurant-restaurant menu association is Integer. So when I go to create a restaurant menu (RestMenu), it says there is an error because the restaurant id I try to use create the menu is a string. Here are my models for my restaurant menus and for my restaurants.
Restaurant Model:
const { Model, DataTypes } = require('sequelize');
const sequelize = require("../config/connection");
class Restaurant extends Model {}
Restaurant.init(
{
restaurant_id: {
type: DataTypes.STRING,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
location: {
type: DataTypes.STRING,
allowNull: false,
}
},
{
sequelize,
}
)
module.exports = Restaurant;
Restaurant Menu Model:
const { Model, DataTypes } = require('sequelize');
const sequelize = require("../config/connection");
class RestMenu extends Model {}
RestMenu.init(
{
menu_id: {
type: DataTypes.STRING,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
}
},
{
sequelize,
}
)
module.exports = RestMenu;
So basically my problem is that I can’t add RestMenus to my database because the database is set up to hold an integer for the restaurant_id column for RestMenus but I can’t figure out why it’s not a string like the rest of my associations are.
Also I have the sync force true on my server file so it is generating a new database when I restart the server.
sequelize.sync({ force: true }).then(function() {
httpServer.listen(PORT, function() {
console.log('App listening on PORT ' + PORT);
});
});
Am I doing something unintentionally that is setting the foreign key to an integer? Or is there someway to manually override the foreign key type to a string?