对 sequelize seeder 的自定义查询

Custom query on sequelize seeder

谁知道如何在 sequelize seeder

上自定义 select 查询

我尝试了两种方法,但没有一种有效

第一次尝试

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

然后出现错误

SequelizeDatabaseError: column "admin" does not exist

我不明白为什么管理员是列在这里???

第二次尝试

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

出现以下错误

SequelizeDatabaseError: syntax error at or near ":"

第三次尝试

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

错误:

SyntaxError: missing ) after argument list

已更新

第四次尝试

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

出现另一个错误:

    SequelizeDatabaseError: relation "Users" does not exist

queryInterface.sequelize.query('SELECT * FROM "Users"') 没有任何错误。我认为这里的问题是 WHERE querying

快把我逼疯了:)

提前感谢您的帮助!

仔细阅读Sequelize文档后,我找到了解决这个问题的方法。 Sequelize raw queries replacements。如果您遇到同样的问题,请尝试以下解决方案

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