Skip to content

Sqlite syntax error #9813

@meanjoe45

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions