在没有模型的情况下在Sequelize中制作新模型:生成?

如果我通过复制粘贴制作模型可以吗?例如,我通过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

http://img3.mukewang.com/62eb2b3c0001bafb08850348.jpg

萧十郎
浏览 115回答 1
1回答

LEATH

根据上面的评论,我意识到我的文件(由Sequelize生成)没有查找放置在子文件夹中的模型文件。相反,它只查找根文件夹中的模型文件 - 与文件级别相同。index.jsindex.js我也不得不使用包来使文件查找子文件夹。globindex.js所以我现在的内容是:index.js'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 = {};const glob = require("glob")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);}const files = glob.sync(__dirname + "/*/*.js")files.forEach(file => {  const model = sequelize['import'](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;为了回答具体问题,不,我不必一直做命令。我可以复制 -粘贴-编辑新模型。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript