Query Interface

An instance of Sequelize uses something called Query Interface to communicate to the database in a dialect-agnostic way. Most of the methods you’ve learned in this manual are implemented with the help of several methods from the query interface.

The methods from the query interface are therefore lower-level methods; you should use them only if you do not find another way to do it with higher-level APIs from Sequelize. They are, of course, still higher-level than running raw queries directly (i.e., writing SQL by hand).

This guide shows a few examples, but for the full list of what it can do, and for detailed usage of each method, check the QueryInterface API.

Obtaining the query interface

From now on, we will call queryInterface the singleton instance of the QueryInterface class, which is available on your Sequelize instance:

  1. const { Sequelize, DataTypes } = require('sequelize');
  2. const sequelize = new Sequelize(/* ... */);
  3. const queryInterface = sequelize.getQueryInterface();

Creating a table

  1. queryInterface.createTable('Person', {
  2. name: DataTypes.STRING,
  3. isBetaMember: {
  4. type: DataTypes.BOOLEAN,
  5. defaultValue: false,
  6. allowNull: false
  7. }
  8. });

Generated SQL (using SQLite):

  1. CREATE TABLE IF NOT EXISTS `Person` (
  2. `name` VARCHAR(255),
  3. `isBetaMember` TINYINT(1) NOT NULL DEFAULT 0
  4. );

Note: Consider defining a Model instead and calling YourModel.sync() instead, which is a higher-level approach.

Adding a column to a table

  1. queryInterface.addColumn('Person', 'petName', { type: DataTypes.STRING });

Generated SQL (using SQLite):

  1. ALTER TABLE `Person` ADD `petName` VARCHAR(255);

Changing the datatype of a column

  1. queryInterface.changeColumn('Person', 'foo', {
  2. type: DataTypes.FLOAT,
  3. defaultValue: 3.14,
  4. allowNull: false
  5. });

Generated SQL (using MySQL):

  1. ALTER TABLE `Person` CHANGE `foo` `foo` FLOAT NOT NULL DEFAULT 3.14;

Removing a column

  1. queryInterface.removeColumn('Person', 'petName', { /* query options */ });

Generated SQL (using PostgreSQL):

  1. ALTER TABLE "public"."Person" DROP COLUMN "petName";

Changing and removing columns in SQLite

SQLite does not support directly altering and removing columns. However, Sequelize will try to work around this by recreating the whole table with the help of a backup table, inspired by these instructions.

For example:

  1. // Assuming we have a table in SQLite created as follows:
  2. queryInterface.createTable('Person', {
  3. name: DataTypes.STRING,
  4. isBetaMember: {
  5. type: DataTypes.BOOLEAN,
  6. defaultValue: false,
  7. allowNull: false
  8. },
  9. petName: DataTypes.STRING,
  10. foo: DataTypes.INTEGER
  11. });
  12. // And we change a column:
  13. queryInterface.changeColumn('Person', 'foo', {
  14. type: DataTypes.FLOAT,
  15. defaultValue: 3.14,
  16. allowNull: false
  17. });

The following SQL calls are generated for SQLite:

  1. PRAGMA TABLE_INFO(`Person`);
  2. CREATE TABLE IF NOT EXISTS `Person_backup` (
  3. `name` VARCHAR(255),
  4. `isBetaMember` TINYINT(1) NOT NULL DEFAULT 0,
  5. `foo` FLOAT NOT NULL DEFAULT '3.14',
  6. `petName` VARCHAR(255)
  7. );
  8. INSERT INTO `Person_backup`
  9. SELECT
  10. `name`,
  11. `isBetaMember`,
  12. `foo`,
  13. `petName`
  14. FROM `Person`;
  15. DROP TABLE `Person`;
  16. CREATE TABLE IF NOT EXISTS `Person` (
  17. `name` VARCHAR(255),
  18. `isBetaMember` TINYINT(1) NOT NULL DEFAULT 0,
  19. `foo` FLOAT NOT NULL DEFAULT '3.14',
  20. `petName` VARCHAR(255)
  21. );
  22. INSERT INTO `Person`
  23. SELECT
  24. `name`,
  25. `isBetaMember`,
  26. `foo`,
  27. `petName`
  28. FROM `Person_backup`;
  29. DROP TABLE `Person_backup`;

Other

As mentioned in the beginning of this guide, there is a lot more to the Query Interface available in Sequelize! Check the QueryInterface API for a full list of what can be done.