Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Feature Request: Native UNION Query Support #18107

@agamennone-siae

Description

@agamennone-siae

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Feature Description

Add native support for UNION (and optionally UNION ALL) queries in Sequelize. Currently, developers must use raw SQL to combine result sets from different models.

I propose a top-level method, sequelize.union(), that accepts an array of queries with models and options, returning a single combined result set.

Describe the feature you'd like to see implemented

Car.init({
  name: DataTypes.STRING,
  type: DataTypes.STRING,
  topSpeed: DataTypes.INTEGER // Attribute specific to Car
}, { sequelize, modelName: 'Car' });

Plane.init({
  name: DataTypes.STRING,
  engineType: DataTypes.STRING,
  maxAirSpeed: DataTypes.INTEGER // Attribute specific to Plane
}, { sequelize, modelName: 'Plane' });

// --- PROPOSED FEATURE ---
// A polymorphic UNION helper that accepts an object with 'queries' and global 'options'
const results = await sequelize.union({
  queries: [
    {
      model: Car,
      options: {
        // Aliasing attributes to match a common interface in the Union result
        attributes: ['name', ['topSpeed', 'velocity']],
        where: { type: 'sedan' }
      }
    },
    {
      model: Plane,
      options: {
        attributes: ['name', ['maxAirSpeed', 'velocity']],
        where: { engineType: 'jet' }
      }
    }
  ],
  // Options applied to the final UNION result (e.g. ORDER BY on the combined set)
  options: {
    unionAll: false, // Default to UNION. If true, uses UNION ALL
    order: [['velocity', 'DESC']],
    limit: 10
  }
});

console.log(results);
// Expected Output (Ordered by 'velocity' across both tables):
// [
//   { name: 'Fighter Jet', velocity: 1500 },
//   { name: 'Private Jet', velocity: 900 },
//   { name: 'Sports Car', velocity: 200 },
//   { name: 'Family Car', velocity: 120 }
// ]

The expected query generated is:

SELECT * FROM (
  SELECT "name", "topSpeed" AS "velocity"
  FROM "Cars"
  WHERE "type" = 'sedan'
  
  UNION
  
  SELECT "name", "maxAirSpeed" AS "velocity"
  FROM "Planes"
  WHERE "engineType" = 'jet'
) AS "UnionQuery"
ORDER BY "velocity" DESC
LIMIT 10;

Describe why you would like this feature to be added to Sequelize

  1. Stay fully within the Sequelize API for complex queries.

  2. Improve type safety and query construction versus raw SQL.

  3. Standardize UNION handling across dialects.

Is this feature dialect-specific?

  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s):

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in implementing my feature.

Indicate your interest in the addition of this feature by adding the 👍 reaction. Comments such as "+1" will be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions