Sequelize添加限制以查询

时间:2017-06-27 07:18:38

标签: node.js sequelize.js

我正在尝试用Sequelize形成一个查询。我需要限制结果,而是我得到错误:“表'表演者'缺少FROM子句条目”。可以预料,因为限制是一个单独的请求。要使用“子查询:false ”是不可能的,因为限制无法正常工作。

查询代码:

  // find the events for the given venues, performers, ParentEvent and date
  return this._events.findAll({
    where: {
      $or: [
        { short_desc: { ilike: '%' + props.keyword + '%' } },
        { search_terms: { ilike: '%' + props.keyword + '%' } },
        { '$Performers.name$': { ilike: '%' + props.keyword + '%' } },
        { '$ParentEvent.name$': { ilike: '%' + props.keyword + '%' } },
        { '$ParentEvent.search_terms$': { ilike: '%' + props.keyword + '%' } },
        { '$Venue.name$': { ilike: '%' + props.keyword + '%' } },
        { '$Venue.previous_name$': { ilike: '%' + props.keyword + '%' } },
        { '$Venue.search_terms$': { ilike: '%' + props.keyword + '%' } }
      ],
      date_start: { $gt: new Date() },
      $and: [ { '$Venue.suburb$': props.suburb }, { '$Venue.postcode$': props.postcode } ]
    },
    include: [
      { model: SubCategory },
      { model: Venue },
      { model: Performer },
      { model: ParentEvent }
    ],
    offset: page * limit,
    limit: limit
  });
})
.then( events => ( events ) )

表演者模特:

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Performer = sequelize.define('Performer', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV1,
      primaryKey: true
    },
    name: DataTypes.STRING,
    description: DataTypes.TEXT,
    short_name: DataTypes.STRING,
    search_terms: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        Performer.belongsToMany( models.Event, {
          through: "PerformerEvents"
        });

        Performer.belongsTo( models.SubCategory );
      }
    }
  });
  return Performer;
};

活动模式:

    'use strict';
module.exports = function(sequelize, DataTypes) {
  var Event = sequelize.define('Event', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV1,
      primaryKey: true
    },
    short_desc: DataTypes.STRING,
    slug: DataTypes.STRING,
    date_start: DataTypes.DATE,
    date_end: DataTypes.DATE,
    kids_scale: DataTypes.INTEGER,
    search_terms: DataTypes.STRING,
    VenueId: { type: DataTypes.UUID, },
    ParentEventId: { type: DataTypes.UUID },
  }, {
    classMethods: {
      associate: function(models) {
        Event.belongsToMany( models.Performer, {  through: "PerformerEvents" } );
        Event.belongsToMany( models.SubCategory, { through: "EventSubCats", } );
        Event.belongsTo( models.Venue, { foreignKey: 'VenueId', onDelete: 'CASCADE', onUpdate: 'CASCADE' } );
        Event.belongsTo( models.ParentEvent, {foreignKey: 'ParentEventId', onDelete: 'CASCADE', onUpdate: 'CASCADE' } );
      }
    }
  });
  return Event;
};

生成sql:

SELECT "Event".*,
       "SubCategories"."id" AS "SubCategories.id",
       "SubCategories"."name" AS "SubCategories.name",
       "SubCategories"."CategoryId" AS "SubCategories.CategoryId",
       "SubCategories"."createdAt" AS "SubCategories.createdAt",
       "SubCategories"."updatedAt" AS "SubCategories.updatedAt",
       "SubCategories.EventSubCats"."createdAt" AS "SubCategories.EventSubCats.createdAt",
       "SubCategories.EventSubCats"."updatedAt" AS "SubCategories.EventSubCats.updatedAt",
       "SubCategories.EventSubCats"."EventId" AS "SubCategories.EventSubCats.EventId",
       "SubCategories.EventSubCats"."SubCategoryId" AS "SubCategories.EventSubCats.SubCategoryId",
       "Venue"."id" AS "Venue.id",
       "Venue"."name" AS "Venue.name",
       "Venue"."description" AS "Venue.description",
       "Venue"."transport" AS "Venue.transport",
       "Venue"."phone" AS "Venue.phone",
       "Venue"."website" AS "Venue.website",
       "Venue"."street" AS "Venue.street",
       "Venue"."region" AS "Venue.region",
       "Venue"."suburb" AS "Venue.suburb",
       "Venue"."state" AS "Venue.state",
       "Venue"."postcode" AS "Venue.postcode",
       "Venue"."overseas" AS "Venue.overseas",
       "Venue"."lat" AS "Venue.lat",
       "Venue"."lng" AS "Venue.lng",
       "Venue"."previous_name" AS "Venue.previous_name",
       "Venue"."search_terms" AS "Venue.search_terms",
       "Venue"."createdAt" AS "Venue.createdAt",
       "Venue"."updatedAt" AS "Venue.updatedAt",
       "Performers"."id" AS "Performers.id",
       "Performers"."name" AS "Performers.name",
       "Performers"."description" AS "Performers.description",
       "Performers"."website" AS "Performers.website",
       "Performers"."short_name" AS "Performers.short_name",
       "Performers"."search_terms" AS "Performers.search_terms",
       "Performers"."createdAt" AS "Performers.createdAt",
       "Performers"."updatedAt" AS "Performers.updatedAt",
       "Performers"."SubCategoryId" AS "Performers.SubCategoryId",
       "Performers.PerformerEvents"."createdAt" AS "Performers.PerformerEvents.createdAt",
       "Performers.PerformerEvents"."updatedAt" AS "Performers.PerformerEvents.updatedAt",
       "Performers.PerformerEvents"."EventId" AS "Performers.PerformerEvents.EventId",
       "Performers.PerformerEvents"."PerformerId" AS "Performers.PerformerEvents.PerformerId",
       "ParentEvent"."id" AS "ParentEvent.id",
       "ParentEvent"."name" AS "ParentEvent.name",
       "ParentEvent"."location" AS "ParentEvent.location",
       "ParentEvent"."description" AS "ParentEvent.description",
       "ParentEvent"."website" AS "ParentEvent.website",
       "ParentEvent"."data_notes" AS "ParentEvent.data_notes",
       "ParentEvent"."date_start" AS "ParentEvent.date_start",
       "ParentEvent"."date_end" AS "ParentEvent.date_end",
       "ParentEvent"."tickets_url" AS "ParentEvent.tickets_url",
       "ParentEvent"."search_terms" AS "ParentEvent.search_terms",
       "ParentEvent"."createdAt" AS "ParentEvent.createdAt",
       "ParentEvent"."updatedAt" AS "ParentEvent.updatedAt"
FROM
  (SELECT "Event"."id",
          "Event"."short_desc",
          "Event"."slug",
          "Event"."long_desc",
          "Event"."timetable",
          "Event"."website",
          "Event"."date_start",
          "Event"."date_end",
          "Event"."is_exhibition",
          "Event"."reservation_req",
          "Event"."area1",
          "Event"."area2",
          "Event"."is_ticketed",
          "Event"."tickets_url",
          "Event"."price_guide_from",
          "Event"."price_guide_to",
          "Event"."kids_scale",
          "Event"."search_terms",
          "Event"."VenueId",
          "Event"."ParentEventId",
          "Event"."createdAt",
          "Event"."updatedAt"
   FROM "Events" AS "Event"
   WHERE "Event"."date_start" > '2017-06-27 14:05:15.057 +00:00'
     AND ("Event"."short_desc" ILIKE '%Brisb%'
          OR "Event"."search_terms" ILIKE '%Brisb%'
          OR "Performers"."name" ILIKE '%Brisb%'
          OR "ParentEvent"."name" ILIKE '%Brisb%'
          OR "ParentEvent"."search_terms" ILIKE '%Brisb%'
          OR "Venue"."name" ILIKE '%Brisb%'
          OR "Venue"."previous_name" ILIKE '%Brisb%'
          OR "Venue"."search_terms" ILIKE '%Brisb%')
     AND ("Venue"."suburb" = 'Woolloongabba'
          AND "Venue"."postcode" = '4102')
     AND
       (SELECT "EventSubCats"."EventId"
        FROM "EventSubCats" AS "EventSubCats"
        INNER JOIN "SubCategories" AS "SubCategory" ON "EventSubCats"."SubCategoryId" = "SubCategory"."id"
        WHERE ("Event"."id" = "EventSubCats"."EventId")
        LIMIT 1) IS NOT NULL
   LIMIT 20
   OFFSET 0) AS "Event"
INNER JOIN ("EventSubCats" AS "SubCategories.EventSubCats"
            INNER JOIN "SubCategories" AS "SubCategories" ON "SubCategories"."id" = "SubCategories.EventSubCats"."SubCategoryId") ON "Event"."id" = "SubCategories.EventSubCats"."EventId"
LEFT OUTER JOIN "Venues" AS "Venue" ON "Event"."VenueId" = "Venue"."id"
LEFT OUTER JOIN ("PerformerEvents" AS "Performers.PerformerEvents"
                 INNER JOIN "Performers" AS "Performers" ON "Performers"."id" = "Performers.PerformerEvents"."PerformerId") ON "Event"."id" = "Performers.PerformerEvents"."EventId"
LEFT OUTER JOIN "ParentEvents" AS "ParentEvent" ON "Event"."ParentEventId" = "ParentEvent"."id";

sequelize:3.30.4 数据库:postgres

有关如何正确形成此查询的任何建议吗?

0 个答案:

没有答案