SQLite.swift icon indicating copy to clipboard operation
SQLite.swift copied to clipboard

Select from multiple tables throws "No such table"

Open JanC opened this issue 8 years ago • 6 comments

Build Information

  • Include the SQLite.swift version, commit or branch experiencing the issue. 0.11.4
  • Mention Xcode and OS X versions affected. Xcode 9.2, iOS 11
  • How do do you integrate SQLite.swift in your project?
    • Carthage

Hi, I noticed that when selecting all columns from multiple tables, SQLite.swift throws an No such table: exception even if the table does exists and the generated SQL itself works fine

More details are in the SO question

Basically

users.select(users[*], posts[*]).join(posts, on: user_id == users[id])

would throw "No such table: posts"

while this works

users.select(users[*], posts[post_text]).join(posts, on: user_id == users[id])

In both cases, the generated SQL is correct

cheers, Jan

JanC avatar Jan 23 '18 15:01 JanC

Having the same problem.

actionAlias.select(modelTableAlias[*], actionAlias[*])
			.join(JoinType.leftOuter, modelTableAlias, on: actionAlias[ActionDao.modelId] == cast(modelTableAlias[idColumn]) as Expression<String> )
            .filter( actionAlias[ActionDao.modelTable] == tableName)

This worked just fine before.

ghost avatar Aug 15 '18 19:08 ghost

I got the same problem, please fix asap.

nguyenthinam avatar Aug 24 '18 11:08 nguyenthinam

Proposed Solution for this issue #777 I had the same problem, and debugged the SQLite.swift code. In file "Query.swift" in function "columnNamesForQuery(_ query: QueryType)" in the code block following "if column == "*" {" in the loop "for q in queries {" there is a "throw QueryError.noSuchTable(name: namespace)" immediately after the if statement WITHIN the loop. Therefore the loop tests one entry (first table of the join), then always throws. The fix is to delete this mis-placed throw statement, leaving the second throw (outside the loop) in place. (Sorry I can't seem to get indenting to work on the copied code below).

if column == "" { var select = query select.clauses.select = (false, [Expression(literal: "") as Expressible]) let queries = [select] + query.clauses.join.map { $0.query } if !namespace.isEmpty { for q in queries { if q.tableName().expression.template == namespace { try expandGlob(true)(q) continue column } throw QueryError.noSuchTable(name: namespace) <- DELETE THIS LINE! } throw QueryError.noSuchTable(name: namespace) } for q in queries { try expandGlob(query.clauses.join.count > 0)(q) } continue }

ghost avatar Oct 09 '18 15:10 ghost

me too. please fix.

devSC avatar Jan 16 '19 07:01 devSC

any updates on this @stephencelis? The proposed PR seems to fix it

JanC avatar Apr 22 '19 08:04 JanC

The problem happens whenever we make tableName[*] to select all the fields of the table, but this table is not the source of the query, is a table in which the join was made.

An immediate solution, while not solving the problem, is to put all the columns of the table inside the select, as below.

rootTable.select (joinTable.firstColumn,
                  joinTable.secondColumn,
                  joinTable.thirdColumn)
         .join (joinTable, on: joinTable.firstColumn == rootTable.mainColumn)

That will solve.

gilmarcsf avatar Jun 06 '19 20:06 gilmarcsf