When I start my server, soon after establishing a database connection I do this:
var tool = require("./tool"); //created with Sequelize.define()
var requirement = require("./requirement"); //created with Sequelize.define()
tool.belongsToMany(requirement, {through: "toolRequirements"});
requirement.belongsToMany(tool, {through: "toolRequirements"});
tool.sync();
requirement.sync();
I expect to see the join table being created, but it's not there. What am I missing?
Executing (default): CREATE TABLE IF NOT EXISTS `requirements` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): CREATE TABLE IF NOT EXISTS `tools` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255) NOT NULL, `idCode` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`requirements`)
Executing (default): PRAGMA INDEX_LIST(`tools`)
It says in the documentation that the table is created for me. I shouldn't have to manually create one myself.
I am using:
It seems instead of calling .sync()
on every single model, I should just call sequelize.sync()
once.
So that makes:
var tool = require("./tool"); //created with Sequelize.define()
var requirement = require("./requirement"); //created with Sequelize.define()
tool.belongsToMany(requirement, {through: "toolRequirements"});
requirement.belongsToMany(tool, {through: "toolRequirements"});
sequelize.sync();
//DO NOT tool.sync();
//DO NOT requirement.sync();
Here is an example of how I used it in my project: link