phalcon/cphalcon · error · JoinAliasAlreadyUsed

Cannot use ' ' as join alias because it was already used…

Error message

Cannot use '{alias}' as join alias because it was already used, when preparing: {phql}

What it means

Thrown in Query::getJoins while processing join aliases: an alias given to a JOIN is already registered in joinModels (used by the FROM clause or an earlier join), so the same alias would map to two models. The input at fault is the duplicate alias name in the PHQL.

Solutions

  1. Give the join a unique alias, e.g. JOIN App\Models\Roles AS r2 when r is taken.
  2. Rename the conflicting alias in the SELECT or WHERE clauses accordingly.
  3. Review generated PHQL for alias reuse when building queries programmatically.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phalcon/Mvc/Model/Query.zep:2641 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/e0f62ec84e09bdc6. Report an issue: GitHub.

Appendix: source

Thrown at phalcon/Mvc/Model/Query.zep:2641

                realModelName = joinData["modelName"],
                completeSource = [source, schema];

            /**
             * Check join alias
             */
            let joinType = this->getJoinType(joinItem);

            /**
             * Process join alias
             */
            if fetch aliasExpr, joinItem["alias"] {
                let alias = aliasExpr["name"];

                /**
                 * Check if alias is unique
                 */
                if unlikely isset joinModels[alias] {
                    throw new JoinAliasAlreadyUsed(alias, this->phql);
                }

                /**
                 * Add the alias to the source
                 */
                let completeSource[] = alias;

                /**
                 * Set the join type
                 */
                let joinTypes[alias] = joinType;

                /**
                 * Update alias: alias
                 */
                let sqlAliases[alias] = alias;

                /**

View on GitHub (pinned to b7419de9cd)