简体   繁体   中英

Sequelize Error: Unknown structure passed to order / group: Literal When Ordering

I'm doing a fairly simple Select and Order By query using Sequelize v6, no joins or anything. The strange thing is it works the first time, but fails the second time I try this no matter what method I try. By that, I mean I've tried the db.literal method and the plain order: ['column', 'DESC'] method.

Here's what it outputs the first time, when it works:

Executing (default): SELECT "id", "timeStart", "timeEnd", "nextPage", "createdAt", "updatedAt" FROM "ScrapeJobs" AS "ScrapeJob" ORDER BY "ScrapeJob"."createdAt" DESC LIMIT 1;
21
const db = require('./connection.js');
const models = require('./models/index');
const ScrapeJob = models.ScrapeJob;
const app = express();

app.get('/snap', async function (req, res) {
  await db.authenticate();
  const q = db.literal('"ScrapeJob"."createdAt" DESC')
  const p = ['createdAt', 'DESC']

  let scrapeJobs = await ScrapeJob.findAll({
    order: [q],
    limit: 1
  })
  console.log(scrapeJobs)

  return res.status(200).send('okay dokay')
})

So this works the first time, everytime. But when I try to trigger this function a second time (by visiting the local URL) it gives me the following error if I use the db.literal method:

Error: Unknown structure passed to order / group: Literal { val: '"ScrapeJob"."createdAt" DESC' }

and this if i use the simple order array:

 Error: Unknown structure passed to order / group: Literal { val: ' DESC' }

I've seen related issues with GroupBy and the associated hacks to get it that to work, but I feel like this is a much simpler case that could possibly have a much simpler solution? I've tried naming my table and schema without much avail, but maybe I did it wrong? Here's my model definition file for reference, without explicit table name or schema name:

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class ScrapeJob extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  };
  ScrapeJob.init({
    timeStart: DataTypes.DATE,
    timeEnd: DataTypes.DATE,
    nextPage: DataTypes.INTEGER
  }, {
    sequelize,
    modelName: 'ScrapeJob'
  });
  return ScrapeJob;
};

I would also be happy with a solution to use raw SQL with Sequelize, but it persistently gives me

SequelizeDatabaseError: relation "ScrapeJobs" does not exist

errors. I've tried quoting, not quoting, prepending the table name with public. and I just cannot seem to get it to query. This is such a simple query and it's giving me hours of headache. Really second-guessing using Sequelize at this point. Can anyone point me in the right direction, whether it means fixing the order clause or just fixing my raw SQL query? Thanks in advance.

I came across this same error when upgrading from Sequelize 5.17.2 to 6.19.2. I had this style group in a few queries:

group: [ [ models.Location, 'ClientId' ] ]

I found this thread: https://github.com/sequelize/sequelize/issues/11421 and followed the suggestion about rewriting the group by statements. I replaced the above with:

group: [ 'Location.ClientId' ]

Now works fine. Might help someone...

Half Answer:

The reason my raw SQL query was not working was because I was making a connection to the production database, which did not yet have the migrations run yet. Stupid mistake. I found this out by printing process.env.DB_HOST which I'd hardcoded as the production db.

However, even after making the migration and being connected to the right database, I persistently get the Error: Unknown structure passed to order / group: Literal { val: ' DESC' } error when making the query:

  let scrapeJobs = await ScrapeJob.findAll({
    order: [['createdAt', 'DESC']],
    limit: 1
  })

I am able to continue with my work, and I guess I'll proceed with raw SQL as long as I'm using Sequelize, but if anyone has any insights on the remaining error I'd still be appreciative.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM