Skip to content

Invalid SQL queries when joining query with Array of referenced Objects #103

Open
@BlobCodes

Description

@BlobCodes

I'm trying to query a User and its groups.
This generates an invalid SQL Query:

SELECT * FROM users INNER JOIN groups AS groups ON groups.id IN users.group_ids

..resulting in runtime errors.
This is a minimal representation of what I'm trying to do (and how you can reproduce the error):

require "sqlite3"
require "onyx/sql"
require "onyx-sql/converters/sqlite3"

class User
  include Onyx::SQL::Model

  schema users do
    pkey id : Int32, converter: SQLite3::Any(Int32)
    type groups : Array(Group), key: "group_ids", converter: SQLite3::Any(Group)
  end
end

class Group
  include Onyx::SQL::Model

  schema groups do
    pkey id : Int32, converter: SQLite3::Any(Int32)
  end
end

puts Onyx::SQL.query(
  User.select("*").join(groups: true) do |x|
  end
)

This results in the following errors:
SQLite3 Error:

Unhandled exception: no such table: users.group_ids (SQLite3::Exception)
  from lib/sqlite3/src/sqlite3/statement.cr:81:5 in 'check'
  from lib/sqlite3/src/sqlite3/statement.cr:4:5 in 'initialize'
  from lib/sqlite3/src/sqlite3/statement.cr:2:3 in 'new'
  from lib/sqlite3/src/sqlite3/connection.cr:24:5 in 'build_prepared_statement'
  from lib/db/src/db/connection.cr:7:15 in 'fetch_or_build_prepared_statement'
  from lib/db/src/db/session_methods.cr:58:9 in 'build'
  from lib/db/src/db/pool_prepared_statement.cr:37:16 in 'build_statement'
  from lib/db/src/db/pool_prepared_statement.cr:53:22 in 'initialize'
  from lib/db/src/db/pool_prepared_statement.cr:11:5 in 'new'
  from lib/db/src/db/database.cr:89:7 in 'build_prepared_statement'
  from lib/db/src/db/database.cr:7:15 in 'fetch_or_build_prepared_statement'
  from lib/db/src/db/session_methods.cr:23:9 in 'build'
  from lib/db/src/db/query_methods.cr:38:7 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:9:11 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:23:7 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:34:7 in 'query'
  from lib/onyx/src/onyx/sql.cr:21:5 in 'query'
  from src/test.cr:24:1 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:97:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:86:7 in 'main'
  from /usr/lib/crystal/crystal/main.cr:106:3 in 'main'
  from __libc_start_main
  from _start
  from ???

PG error:

Unhandled exception: syntax error at or near "users" (PQ::PQError)
  from lib/pg/src/pq/connection.cr:204:7 in 'handle_error'
  from lib/pg/src/pq/connection.cr:0:9 in 'handle_async_frames'
  from lib/pg/src/pq/connection.cr:163:7 in 'read'
  from lib/pg/src/pq/connection.cr:158:7 in 'read'
  from lib/pg/src/pq/connection.cr:314:31 in 'expect_frame'
  from lib/pg/src/pq/connection.cr:313:5 in 'expect_frame'
  from lib/pg/src/pg/statement.cr:18:5 in 'perform_query'
  from lib/db/src/db/statement.cr:103:14 in 'perform_query_with_rescue'
  from lib/db/src/db/statement.cr:88:7 in 'query'
  from lib/db/src/db/pool_statement.cr:39:30 in 'query'
  from lib/db/src/db/query_methods.cr:38:7 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:9:11 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:23:7 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:34:7 in 'query'
  from lib/onyx/src/onyx/sql.cr:21:5 in 'query'
  from src/bin/coleus.cr:73:1 in '__crystal_main'
  from /usr/lib/crystal/crystal/main.cr:97:5 in 'main_user_code'
  from /usr/lib/crystal/crystal/main.cr:86:7 in 'main'
  from /usr/lib/crystal/crystal/main.cr:106:3 in 'main'
  from __libc_start_main
  from _start
  from ???

Here is a SQLite3 DB to test it:
test.db.zip

Just install the shards sqlite3 and onyx-sql and run DATABASE_URL="sqlite3://./test.db" crystal src/test.cr to test it yourself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions