I'm running into an error where when I do a get request in node to my database, I'm getting an empty array. So I look in the console log and it says it's dropping all tables if exist and then creating them but the seed file is not running. I can run "npm run seed" outside of the server and it will seed the database but it keeps dropping and not seeding when I run node server.js. I even tried putting the path to the seed inside the packages.json "start": to make both the seed and server run on start but no luck. The app was working perfectly but one of my team members accidentally pushed to the master and it messed up the app I don't know what's messed up because this was working before the git messup. I've created a new repo though now and getting same problem.
//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();
});
});
});
//console
Executing (default): DROP TABLE IF EXISTS MarketProduct
; Executing (default): DROP TABLE IF EXISTS Product
; Executing (default): DROP TABLE IF EXISTS Markets
; Executing (default): DROP TABLE IF EXISTS Markets
; Executing (default): 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; Executing (default): SHOW INDEX FROM Markets
FROM grocerEZ_db
Executing (default): DROP TABLE IF EXISTS Product
; Executing (default): 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; Executing (default): SHOW INDEX FROM Product
FROM grocerEZ_db
Executing (default): DROP TABLE IF EXISTS MarketProduct
; Executing (default): CREATE TABLE IF NOT EXISTS MarketProduct
( createdAt
DATETIME NOT NULL, updatedAt
DATETIME NOT NULL, MarketId
INTEGER , ProductId
INTEGER , PRIMARY KEY ( MarketId
, ProductId
), FOREIGN KEY ( MarketId
) REFERENCES Markets
( id
) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY ( ProductId
) REFERENCES Product
( id
) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB; Executing (default): SHOW INDEX FROM MarketProduct
FROM grocerEZ_db
App listening on PORT 3000
Just change
db.sequelize.sync({ force: true }) // <-- Forces table to drop and create again
to
db.sequelize.sync({ force: false , alter : true })
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); });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.