Skip to content

Sqlite syntax error #9813

Closed
Closed

Description

@meanjoe45

What are you doing?

I have a set of models/associations below that generate a query that produces a SQL error (testing with sqlite). This is as far as I could boil it down and still produce the error.

'use strict';

const Sequelize = require('sequelize');
console.log(Sequelize.version);

const sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', operatorsAliases: false, logging: console.log });

const Project = sequelize.define('Project', { name: Sequelize.STRING });
const LevelTwo = sequelize.define('LevelTwo', { name: Sequelize.STRING });
const LevelThree = sequelize.define('LevelThree', { type: Sequelize.INTEGER });

Project.hasMany(LevelTwo);
LevelTwo.belongsTo(Project);

LevelTwo.hasMany(LevelThree, { as: 'type_ones' });
LevelTwo.hasMany(LevelThree, { as: 'type_twos' });
LevelThree.belongsTo(LevelTwo);

function callQuery() {
    return Project.find({
        where: { id: 1 },
        include: [
            {
                model: LevelTwo,
                include: [
                    {
                        model: LevelThree,
                        as: 'type_ones',
                        where: { type: 0 },
                        separate: true,
                        limit: 1,
                        order: [['createdAt', 'DESC']]
                    },
                    {
                        model: LevelThree,
                        as: 'type_twos',
                        where: { type: 1 },
                        separate: true,
                        limit: 1,
                        order: [['createdAt', 'DESC']]
                    }
                ]
            }
        ]
    });
}

sequelize.sync()
    .then(function () {
        return Sequelize.Promise.all([
            Project.create({ name: 'testProject' }),
            LevelTwo.create({ name: 'testL2-1' }),
            LevelTwo.create({ name: 'testL2-2' })
        ]);
    })
    .bind({})
    .spread(function (project, L2_1, L2_2) {
        this.project = project;
        this.L2_1 = L2_1;
        this.L2_2 = L2_2;

        return this.project.addLevelTwo(this.L2_1);
    })
    .then(function () {
        return callQuery();
    })
    .then(function (result) {
        console.log(result.get({ plain: true }));

        return this.project.addLevelTwo(this.L2_2);
    })
    .then(function () {
        return callQuery();
    })
    .then(function (result) {
        console.log(result.get({ plain: true }));
    });

What do you expect to happen?

The above is just a base find query. The part I assume is a little more rare is I'm requesting the same table twice but the queries differ specifying the the type column of the table. I'm simply expecting the query to function as constructed.

What is actually happening?

The output on the second call to callQuery is the following error. If the limit: 1 option is removed from the LevelThree model include object, the error goes away. I'm not sure exactly why that is, but maybe it will help (and is my current work around).

Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: near "UNION": syntax error

Dialect: sqlite (only dialect tested)
Dialect version: 4.0.2 (npm sqlite3 package)
Sequelize version: 4.38.0
Tested with latest release: Yes, 4.38.0
Node.js version: 6.14.2

Thanks for the help and the great work on Sequelize.

Activity

  1. added
    type: bug
    on Aug 28, 2018
  2. internalfx commented on Sep 27, 2018

    @internalfx

    +1

  3. tzellman commented on Sep 28, 2018

    @tzellman

    I came across this same issue. I'm not sure if is actually an issue with sequelize or with sqlite.

    The reason this is failing is because of the extra parentheses around the union clauses.

    To demonstrate the issue, go here: http://www.sqlitetutorial.net/tryit/query/sqlite-union/#3

    This query works:

    SELECT `USERS`.* FROM (
    SELECT
    	firstname,
    	lastname
    FROM
    	employees
    UNION
    SELECT
    	firstname,
    	lastname
    FROM
    	customers
    
    ORDER BY
    	firstname,
    	lastname
    ) AS `USERS`;

    This query does NOT work (and is akin to what sequelize generates for unions):

    SELECT `USERS`.* FROM (
    (SELECT
    	firstname,
    	lastname
    FROM
    	employees)
    UNION
    (SELECT
    	firstname,
    	lastname
    FROM
    	customers
    
    ORDER BY
    	firstname,
    	lastname
    )) AS `USERS`;

    Notice the extra parentheses around the union clauses.

    The error returned from this is: Uncaught Error: near "UNION": syntax error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions