如果我通过复制粘贴制作模型可以吗?例如,我通过Sequelize的制作了我的第一个模型。然后对于其他模型,我只是将模型的所有内容复制粘贴到我的新模型。Usermodel:generateUser
执行数据库同步时:
db.sequelize.sync({ force : true}).then(() => {
console.log("Drop and re-sync db.")
})
新模型不会创建新表。所以我认为复制粘贴新模型是行不通的。这是对的吗?
用户型号:
'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;
};
并且从复制粘贴+修改用户模型生成,我有:国家模型
'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;
};
索引.js由我的模型中的 cli 生成:
'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);
}
});
然后在生成模型后,在执行后,我将生成的User.js转移到User文件夹中。然后进行复制粘贴,不确定移动文件是否会影响我通过复制粘贴创建的模型的注册。model:generate
LEATH
相关分类