当 运行 节点 server.js 和重新播种数据库时,表被删除。续集

Tables getting dropped when running node server.js and re-seeding db. Sequelize

我 运行 遇到一个错误,当我在节点中对我的数据库执行获取请求时,我得到一个空数组。所以我查看控制台日志,它说它正在删除所有表(如果存在)然后创建它们,但种子文件不是 运行ning。我可以 运行 "npm run seed" 在服务器外部,它会为数据库播种,但当我 运行 节点 server.js 时,它会不断下降而不是播种。我什至尝试将种子的路径放在 packages.json "start" 中:使种子和服务器 运行 都启动但没有运气。该应用程序运行良好,但我的一名团队成员不小心将其推向了 master,它搞砸了该应用程序我不知道搞砸了什么,因为它在 git 混乱之前工作。我现在创建了一个新的回购协议并遇到了同样的问题。

 //package.json
 "start": "node server.js ./seeds/seed-db.js"


    //seed-db.js
     const db = require("../models");
    const productSeeds = require("./seed-products.json");

     db.sequelize.sync({ force: true }).then(function() {
      db.Market.bulkCreate([
     {
  name: "Cotton Mill Farmers Market",
  address: "401 Rome St.",
  city: "Carrollton",
  state: "GA",
  zip: "30117"
 }
    ]).then(function(dbMarkets) {
//COTTON MILL FARMERS MARKET STOCK
   dbMarkets[0].createProduct(productSeeds.broccoli).then(function() {
  db.sequelize.close();
   });


dbMarkets[1].createProduct(productSeeds.broccoli).then(function() 
 {
  db.sequelize.close();
      });
       });
    });

//控制台

正在执行(默认):DROP TABLE IF EXISTS MarketProduct; 正在执行(默认):DROP TABLE IF EXISTS Product; 正在执行(默认):DROP TABLE IF EXISTS Markets; 正在执行(默认):DROP TABLE IF EXISTS Markets; 执行(默认):CREATE TABLE IF NOT EXISTS Markets (id INTEGER NOT NULL auto_increment , name VARCHAR(255), address VARCHAR (255), city VARCHAR(255), state VARCHAR(255), zip INTEGER, PRIMARY KEY (id)) ENGINE=InnoDB; 正在执行(默认):SHOW INDEX FROM Markets FROM grocerEZ_db 正在执行(默认):DROP TABLE IF EXISTS Product; 执行(默认):CREATE TABLE IF NOT EXISTS Product (id INTEGER NOT NULL auto_increment , name VARCHAR(255), category ENUM ('meat', 'fruits', 'vegetables', 'misc'), isOrganic TINYINT(1), PRIMARY KEY (id)) ENGINE=InnoDB; 正在执行(默认):SHOW INDEX FROM Product FROM grocerEZ_db 正在执行(默认):DROP TABLE IF EXISTS MarketProduct; 正在执行(默认):CREATE TABLE IF NOT EXISTS MarketProduct (createdAt DATETIME NOT NULL, updatedAtDATETIME NOT NULL, MarketId INTEGER , ProductId整数,主键(MarketIdProductId),外键(MarketId)参考Marketsid)在更新级联上删除级联,外键( ProductId) 参考文献 Product (id) 更新级联时删除级联) ENGINE=InnoDB; 正在执行(默认):SHOW INDEX FROM MarketProduct FROM grocerEZ_db 应用监听端口 3000

改变一下

db.sequelize.sync({ force: true }) // <-- Forces table to drop and create again

db.sequelize.sync({ force: false , alter : true })

DOC :

If force is true, each Model will run DROP TABLE IF EXISTS, before it tries to create its own table

Alters tables to fit models. Not recommended for production use. Deletes data in columns that were removed or had their type changed in the model.


NOTE :

db.sequelize.sync , this should run only once , when you want to create / modify the table based upon the changes you have made on models , would suggest to put that in separate file and run it when it is required.

This is how you should run query by checking connection not by running sync ,

db.sequelize
.authenticate()
.then(() => {
    console.log('Connection has been established successfully.');
})
.catch(err => {
    console.error('Unable to connect to the database:', err);
});