Reputation: 10906
So I have to perform a complex migration, and for my version of sequelize I have to use plain old SQL. This happens because in version 3.30 QueryInterface
does not include the addConstraint
and removeConstraint
.
How can I execute a raw SQL query in my migration file?
Migration:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
// queryInterface.query does not exist :S
// queryInterface.raw does not exist :S
return queryInterface.query(' \
ALTER TABLE "people" \
DROP CONSTRAINT "peoplePartners_peopleId_fkey" \
');
},
down: (queryInterface, Sequelize) => {
}
};
Of course, my real query is way more complex than the one shown above, which is for explanation purposes.
How do I perform a raw query in a db migration file in Sequelize 3.30?
Upvotes: 0
Views: 1946
Reputation: 461
Background
The queryInterface
object has sequelize
as a property.
The sequelize
property contains the query
method.
Example
Run a raw SQL query in a migration:
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query(`
ALTER TABLE "people"
DROP CONSTRAINT "peoplePartners_peopleId_fkey"
`);
}
}
Credit to
Unable to execute the raw query in Sequelize migrations
Upvotes: 4