Is it okay if I make models from copy-pasting? For instance, I made my first model User
through Sequelize's model:generate
. And then for other models, I just copy-pasted everything from the User
model to my new models.
When doing db sync:
db.sequelize.sync({ force : true}).then(() => {
console.log("Drop and re-sync db.")
})
The new models won't create new tables. So I'm thinking copy-pasting for new models won't work. Is this right?
User model:
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
id: { type: DataTypes.BIGINT, allowNull: false, unique: true, primaryKey: true },
fbid: { type: DataTypes.BIGINT, allowNull: false, unique: true },
email: DataTypes.STRING,
first_name: DataTypes.STRING,
last_name: DataTypes.STRING,
photo_url: DataTypes.STRING
}, {});
User.associate = function(models) {
// associations can be defined here
};
return User;
};
And generated from copy-paste + modification of user model, I have: Country model
'use strict';
module.exports = (sequelize, DataTypes) => {
const Country = sequelize.define('Country', {
country_name: DataTypes.STRING
}, {});
Country.associate = function(models) {
// associations can be defined here
};
return Country;
};
Index.js generated by the cli in my models:
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
Then after the generated models, after executing model:generate
I transferred the generated User.js into a User folder. Then do the copy-paste, not sure if moving the files affect the registering of the models I created by copy-paste.