Skip to content

Sequelize errors during startup with empty database needing to be seeded #2481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
nstuyvesant opened this issue Jan 31, 2017 · 2 comments
Closed
1 task done

Comments

@nstuyvesant
Copy link
Contributor

  • I understand that GitHub issues are not for tech support, but for questions specific to this generator, bug reports, and feature requests.
Item Version
generator-angular-fullstack 4.1.2
Node 6.9.4
npm 4.1.2
Operating System OS X 10
Item Answer
Transpiler Babel
Markup Pug
CSS SCSS
Router ui-router
Client Tests Mocha
DB Postgres
Auth Y

Was getting a Sequelize error when doing a gulp serve on:
sqldb.sequelize.sync()
in server/app.js
causing startServer to not be called. Turns out the issue was with:

if(config.seedDB) {
  require('./config/seed');
}

Since seed.js calls .sync() and app.js also calls .sync(), sequelize was doubling up the queries causing a conflict in the creation of the sequence for the User table in PostgreSQL.

To resolve, I modified app.js slightly by removing the conditional above then changing:

sqldb.sequelize.sync()
  .then(startServer)
  .catch(function(err) {
    console.log('Server failed to start due to error: ', err);
  });

to

// Sync the database which will seed it (if appropriate) then startServer
sqldb.sequelize.sync()
  .then(seedDatabaseIfNeeded)
  .then(startServer)
  .catch(err => console.log('Server failed to start due to error: ', err));

then modified server/config/seed.js to start with:

import config from './environment/';
import sqldb from '../sqldb';

export default function seedDatabaseIfNeeded() {
  if(config.seedDB) { // Only run if enabled
    // Original code to return the promise for User.destroy() and User.bulkCreate()
  }
}

To reproduce, scaffold a fresh project with angular-fullstack including authentication with sequelize, configure it to connect to PostgreSQL 9.6.1, turn on sequelize logging and you should see duplicated queries being run during the initial seeding.

@Awk34
Copy link
Member

Awk34 commented Jan 31, 2017

Please submit a Pull Request for this

@nstuyvesant
Copy link
Contributor Author

All set (my first PR)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants