dolthub / dolt

Dolt – Git for Data
Apache License 2.0
17.97k stars 513 forks source link

Column could not be found when using fully qualified table name in `ORDER BY` #6773

Closed luca1197 closed 1 year ago

luca1197 commented 1 year ago

When trying to run this simple query in Dolt using Prisma:

const foo = await prisma.tablename.findMany({
  select: {
    id: true,
    firstName: true,
    // ...
  },
  orderBy: {
    firstName: 'desc',
  },
})

Prisma generates this SQL query:

SELECT `database`.`tablename`.`id`, `database`.`tablename`.`firstName` FROM `database`.`tablename` WHERE 1=1 ORDER BY `database`.`tablename`.`firstName` DESC

which causes Dolt to throw an error: ERROR [no conn] unable to prepare query: column "firstName" could not be found in any table in scope {query=SELECT ....

When manually replacing the ORDER BY statement with just the column name (ORDER BY database.tablename.firstNameORDER BY firstName [backticks removed due to markdown formatting]), the query is working fine.

Since ordering is a very common operation and there does not seem to be a good way to not use fully qualified table names on the Prisma-side, it would be great if Dolt would support this.

timsehn commented 1 year ago

A simple query like this also fails:

select * from t order by db.t.pk;

@JCOR11599 is working on this and we'll try and get it fixed by latest tomorrow.

jycor commented 1 year ago

Hey @luca1197, thanks for reporting this bug. The fix for this has been merged to dolt main. Expect a release with the fix later today!

This issue actually led to the discovery of another related bug involving joining tables with the same name across different databases (currently working on that).