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

How to execute custom SQL in migration? #313

Closed
wuyuntao opened this Issue Nov 6, 2012 · 7 comments

Comments

5 participants
@wuyuntao

wuyuntao commented Nov 6, 2012

ALT TABLE for example.

@noazark

This comment has been minimized.

noazark commented Feb 28, 2013

This is what I have had success with:

module.exports = {
  up: function (migration, DataTypes) {
    migration.migrator.sequelize.query('ALTER TABLE my_table ADD PRIMARY KEY(id)');
  }
}
@janmeier

This comment has been minimized.

Member

janmeier commented Dec 17, 2013

@noazark 's suggestion should be the way to go - remember that the call is async though

@janmeier janmeier closed this Dec 17, 2013

@tipztek

This comment has been minimized.

tipztek commented Apr 11, 2015

And in 2.0.x?

@bakavic

This comment has been minimized.

bakavic commented May 13, 2015

@ebrian The migration being passed in when using sequelize-cli is sequelize.getQueryInterface(). So you can get access back to sequelize with:

module.exports = {
  up: function (migration, DataTypes) {
    return migration.sequelize.query('ALTER TABLE my_table ADD PRIMARY KEY(id)');
  }
}

Edited: realised you should return a promise when migrations are written without the 3rd arugument callback.

@tipztek

This comment has been minimized.

tipztek commented May 13, 2015

@bakavic For whatever reason that didn't work for me. However, var sequelize = require('./sequelize-bootstrap'); worked fine.

@bakavic

This comment has been minimized.

bakavic commented May 14, 2015

@ebrian I can't seem to find sequelize-bootstrap? Trying to require it in my migration file leads to an error complaining that it can't be found. Googling doesn't seem to bring up anything as well.

Just realised that the sequelize-cli readme seems to recommend getting sequelize from the queryInterface passed in as the first arg to the migration function, guess this should be the way forward?

@tipztek

This comment has been minimized.

tipztek commented May 14, 2015

@bakavic It may not be called sequelize-bootstrap, but, whatever file has your bootstrapping logic.

image

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