postgresqlsequelize.jspostgresql-9.4sequelize-cli

Custom query on sequelize seeder


Any one know how to custom select query on sequelize seeder

I have tried two ways, but no one work

First attempt

  up: function(queryInterface, Sequelize) {
    return queryInterface.sequelize.query(
      'SELECT * FROM "Users" WHERE username = "admin"',
      { type: queryInterface.sequelize.QueryTypes.SELECT }
    ).then(function(users) {});
    },

and then got error

SequelizeDatabaseError: column "admin" does not exist

I do not understand why admin is column here ???

Second attempt

return queryInterface.sequelize.query('SELECT * FROM "Users" WHERE username = :admin', {
  replacement: {
    admin: 'admin'
  },
  type: queryInterface.sequelize.QueryTypes.SELECT
}).then(function(users) {
});

Below error occurred

SequelizeDatabaseError: syntax error at or near ":"

Third attempt

return queryInterface.sequelize.query(
  'SELECT * FROM "Users" WHERE username = ' admin '',
  {type: queryInterface.sequelize.QueryTypes.SELECT})
.then(function(users) { })

Error:

SyntaxError: missing ) after argument list

UPDATED

Fourth attempt

    return queryInterface.sequelize.query(
      'SELECT * FROM Users WHERE username = "admin"',
      { type: queryInterface.sequelize.QueryTypes.SELECT }
    ).then(function(users) {});

Another error appear:

    SequelizeDatabaseError: relation "Users" does not exist

queryInterface.sequelize.query('SELECT * FROM "Users"') works without any error. I think the problem here is WHERE querying

It's driving me to crazy :)

Thank you for any help in advance!


Solution

  • I have found solution for this issue after reading Sequelize document carefully. Sequelize raw queries replacements. If you face the same issue, please try following solution

    return queryInterface.sequelize.query(
      'SELECT * FROM "Users" WHERE username = ? ', {
        replacements: ['admin'],
        type: queryInterface.sequelize.QueryTypes.SELECT
      }).then(users => {