Tables getting dropped when running node server.js and re-seeding db. Sequelize
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
javascript mysql node.js sequelize.js seed
add a comment |
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
javascript mysql node.js sequelize.js seed
add a comment |
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
javascript mysql node.js sequelize.js seed
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
javascript mysql node.js sequelize.js seed
javascript mysql node.js sequelize.js seed
asked Nov 10 at 6:16
walkforr
122
122
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Just change
db.sequelize.sync( force: true ) // <-- Forces table to drop and create again
to
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);
);
I couldn't get it work. Still drops tables if exists
– walkforr
Nov 10 at 7:01
@walkforr , please make sure thatsync()
it not written any where else , remove it from all places , and if you want to keep that make itsync( force: false )
, as this is the only thing that make tables drops if exists. and also restart the server once changes are being made.
– Vivek Doshi
Nov 10 at 7:07
after some days of moving around my code, your solution worked. thanks so much. force: false.
– walkforr
Nov 13 at 2:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236477%2ftables-getting-dropped-when-running-node-server-js-and-re-seeding-db-sequelize%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just change
db.sequelize.sync( force: true ) // <-- Forces table to drop and create again
to
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);
);
I couldn't get it work. Still drops tables if exists
– walkforr
Nov 10 at 7:01
@walkforr , please make sure thatsync()
it not written any where else , remove it from all places , and if you want to keep that make itsync( force: false )
, as this is the only thing that make tables drops if exists. and also restart the server once changes are being made.
– Vivek Doshi
Nov 10 at 7:07
after some days of moving around my code, your solution worked. thanks so much. force: false.
– walkforr
Nov 13 at 2:41
add a comment |
Just change
db.sequelize.sync( force: true ) // <-- Forces table to drop and create again
to
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);
);
I couldn't get it work. Still drops tables if exists
– walkforr
Nov 10 at 7:01
@walkforr , please make sure thatsync()
it not written any where else , remove it from all places , and if you want to keep that make itsync( force: false )
, as this is the only thing that make tables drops if exists. and also restart the server once changes are being made.
– Vivek Doshi
Nov 10 at 7:07
after some days of moving around my code, your solution worked. thanks so much. force: false.
– walkforr
Nov 13 at 2:41
add a comment |
Just change
db.sequelize.sync( force: true ) // <-- Forces table to drop and create again
to
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);
);
Just change
db.sequelize.sync( force: true ) // <-- Forces table to drop and create again
to
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);
);
edited Nov 10 at 6:27
answered Nov 10 at 6:18
Vivek Doshi
20.4k22651
20.4k22651
I couldn't get it work. Still drops tables if exists
– walkforr
Nov 10 at 7:01
@walkforr , please make sure thatsync()
it not written any where else , remove it from all places , and if you want to keep that make itsync( force: false )
, as this is the only thing that make tables drops if exists. and also restart the server once changes are being made.
– Vivek Doshi
Nov 10 at 7:07
after some days of moving around my code, your solution worked. thanks so much. force: false.
– walkforr
Nov 13 at 2:41
add a comment |
I couldn't get it work. Still drops tables if exists
– walkforr
Nov 10 at 7:01
@walkforr , please make sure thatsync()
it not written any where else , remove it from all places , and if you want to keep that make itsync( force: false )
, as this is the only thing that make tables drops if exists. and also restart the server once changes are being made.
– Vivek Doshi
Nov 10 at 7:07
after some days of moving around my code, your solution worked. thanks so much. force: false.
– walkforr
Nov 13 at 2:41
I couldn't get it work. Still drops tables if exists
– walkforr
Nov 10 at 7:01
I couldn't get it work. Still drops tables if exists
– walkforr
Nov 10 at 7:01
@walkforr , please make sure that
sync()
it not written any where else , remove it from all places , and if you want to keep that make it sync( force: false )
, as this is the only thing that make tables drops if exists. and also restart the server once changes are being made.– Vivek Doshi
Nov 10 at 7:07
@walkforr , please make sure that
sync()
it not written any where else , remove it from all places , and if you want to keep that make it sync( force: false )
, as this is the only thing that make tables drops if exists. and also restart the server once changes are being made.– Vivek Doshi
Nov 10 at 7:07
after some days of moving around my code, your solution worked. thanks so much. force: false.
– walkforr
Nov 13 at 2:41
after some days of moving around my code, your solution worked. thanks so much. force: false.
– walkforr
Nov 13 at 2:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236477%2ftables-getting-dropped-when-running-node-server-js-and-re-seeding-db-sequelize%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown