nextras / dbal

Database Abstraction Layer – concise secure API to construct queries & fetch data
https://nextras.org/dbal
MIT License
79 stars 28 forks source link

ERROR: column users.id does not exist #56

Closed spisovatelprogramu closed 7 years ago

spisovatelprogramu commented 7 years ago

https://www.postgresql.org/docs/8.1/static/sql-keywords-appendix.html

env

PostgreSQL: 9.6.3

use case


/**
 * @property-read int $id {primary}
 * ...
 * @property ManyHasMany|Project[] $projects {n:n Project::$users}
 */
class User extends Entity {}

/**
 * @property-read int $id {primary}
 * ...
 * @property ManyHasMany|User[] $users {n:n User::$projects, isMain=true}
 */
class Project extends Entity {}

$projectRepo->findBy(['this->users->id' => 2]);
SELECT COUNT(*) FROM "project" AS "project" LEFT JOIN project_x_user AS "project_x_user" ON ("project"."id" = "project_x_user"."project_id") LEFT JOIN user AS "users" ON ("project_x_user"."user_id" = "users"."id") WHERE "users"."id" = 2

error

Nextras\Dbal\QueryException

ERROR:  column users.id does not exist
LINE 1: ... user AS "users" ON ("project_x_user"."user_id" = "users"."i...

possible fixes

            $query .= ' '
-               . $join['type'] . " JOIN {$join['table']} " . ($join['alias'] ? "AS [{$join['alias']}] " : '')
+               . $join['type'] . " JOIN [{$join['table']}] " . ($join['alias'] ? "AS [{$join['alias']}] " : '')
                . 'ON (' . $join['on'] . ')';
hrach commented 7 years ago

Hi, this seems more likely like this bug: https://github.com/nextras/orm/issues/228 which is in Nextras Orm. Will try it ASAP in Orm.

The signature of query builder is leftJoin(string $fromAlias, string $toExpression, string $toAlias, string $onExpression, ...$args): self, e.g. the second parameter may be a whole expression, like subquery - fir left join (select *... ) as alias. So closing it here. Thanks for the report!