Skip to content

Can not execute raw SQL queries for sqlite3 (sequelize.query) #5755

Closed

Description

@907th

I try to use raw SQL queries with sqlite adapter:

    q = db.query(
      `SELECT c.* FROM cities c ....`,
      {
        type: db.QueryTypes.SELECT,
        model: City,
        replacements: { ... }
      }
    );

But I'm getting this error:

Executed (default): SELECT c.* FROM ... in 5ms
/Users/laise/Work/itowl/eps-online-service-countries/node_modules/sqlite3/lib/trace.js:28
                    throw err;
                    ^

TypeError: Cannot read property 'geonameid' of undefined
    at /Users/laise/Work/itowl/eps-online-service-countries/node_modules/sequelize/lib/dialects/sqlite/query.js:161:46
    at /Users/laise/Work/itowl/eps-online-service-countries/node_modules/lodash/lodash.js:12449:23
    at /Users/laise/Work/itowl/eps-online-service-countries/node_modules/lodash/lodash.js:4332:15
    at baseForOwn (/Users/laise/Work/itowl/eps-online-service-countries/node_modules/lodash/lodash.js:2731:24)

SQL is valid and successfully executes.

Furthemore, a query with (undocumented?) raw: true option set executes succesfully:

    q = db.query(
      `SELECT c.* FROM cities c ....`,
      {
        raw: true,
        type: db.QueryTypes.SELECT,
        model: City,
        replacements: { ... }
      }
    );

The source of the issue is type = tableTypes[name] assignment here.
tableTypes is always undefined, because it is being assigned in tableTypes = columnTypes[tableName] line here and columnTypes is always equal to {}.

Dialect: sqlite
Database version: sqlite3
Sequelize version: 3.21.0

Activity

  1. 907th commented on Apr 15, 2016

    @907th
    Author

    This query works too:

        q = db.query(
          `SELECT c.* FROM cities c ....`,
          {
            tableNames: ["cities"],
            type: db.QueryTypes.SELECT,
            model: City,
            replacements: { ... }
          }
        );
    

    Seems like the adapter was not able to deduce a table name from my SQL.
    I was not able to find any documentation on raw and tableNames params.
    I think it needs to be documented somewhere.

  2. 907th commented on Apr 15, 2016

    @907th
    Author

    A full query:

            SELECT c.* FROM ft_search f LEFT JOIN cities c ON f.geonameid == c.geonameid
            WHERE f.names MATCH :prefix AND c.country = :countryAlpha2
            ORDER BY c.population DESC
            LIMIT :count OFFSET :skip
    

    And I see that you handle only FROM ... tables. May be JOIN ... tables should be handled too?

  3. mickhansen commented on Apr 18, 2016

    @mickhansen
    Contributor

    Generally you want to use raw: true, it should probably be the default actually.
    When using non-raw queries sequelize assumes (poorly) that it's an internal call that has all the correct internal values.

  4. added
    type: bug
    on Apr 18, 2016
  5. ivo-stefchev commented on May 20, 2016

    @ivo-stefchev
    Contributor

    sequelize/lib/dialects/sqlite/query.js

    var columnTypes = {};
    ...
       , tableTypes = columnTypes[tableName];
    ...
    var type = tableTypes[name];

    columnTypes never sets any key, hence tableTypes would always be undefined.

  6. witem commented on Nov 16, 2016

    @witem
    Contributor

    Can you fix this in v3 branch?

    In version 3.25.0 line 147:
    , tableTypes = columnTypes[tableName];

  7. felixfbecker commented on Nov 16, 2016

    @felixfbecker
    Contributor

    Feel free to backport

  8. witem commented on Nov 16, 2016

    @witem
    Contributor
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